Is there any way to omit null values?
See original GitHub issueWhat are you doing?
I have a model defined as bellow:
const Scene = sequelize.define(
'scene',
{
sceneId: { type: Sequelize.STRING, allowNull: false, primaryKey: true },
owner: { type: Sequelize.STRING, allowNull: false },
name: Sequelize.STRING,
description: Sequelize.STRING,
width: Sequelize.INTEGER,
height: Sequelize.INTEGER
}
)
When I query one instance use Scene.findOne
and send back to express:
const scene = Scene.findOne({...})
res.json(scene)
I get a scene like this:
{
"sceneId": "12356",
"owner": "sam",
"name": null,
"description": null,
"width": null,
"height": null
}
As null
would break our webapp, I would like to have this:
{
"sceneId": "12356",
"owner": "sam"
}
Is there any way to omit these null values when calling scene.toJSON()
?
To Reproduce Steps to reproduce the behavior:
- Define models X, Y, …
- Run the following
- See error
What do you expect to happen?
I wanted Foo!
What is actually happening?
But the output was bar!
Output, either JSON or SQL
Environment
Dialect:
- mysql
- postgres
- sqlite
- mssql
- any Dialect library version: XXX Database version: XXX Sequelize version: XXX Node Version: XXX OS: XXX If TypeScript related: TypeScript version: XXX Tested with latest release:
- No
- Yes, specify that version:
Issue Analytics
- State:
- Created 4 years ago
- Reactions:3
- Comments:9 (6 by maintainers)
Top Results From Across the Web
SQL Query to Exclude Null Values - GeeksforGeeks
To exclude the null values from the table we need to use IS NOT NULL operator with the WHERE clause. WHERE Clause: The...
Read more >Is there any way to remove the null values which I am getting ...
You could simply map over the values recursively and at each level filter out the false/null values using filter(Boolean) .
Read more >How to remove null values in Tableau - TAR Solutions
There are four ways to hide Null values in Tableau: Format; Filter; Formula; Alias. The first is to Format the nulls, second is...
Read more >Ignore Null Fields with Jackson | Baeldung
This quick tutorial is going to cover how to set up Jackson to ignore null fields when serializing a java class.
Read more >How to remove null values from a column in Tableau
Step 1:- Connect the data set. ; Step 2:- Drop any measure or dimension which contains the NULL values. ; Step 3:- Right-click...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
@knoxcard I found a workaround that using
JSON.stringify(value, replacer)
:if you are using express, you can do like this:
Setup getters in your model…
http://docs.sequelizejs.com/manual/models-definition.html#getters--amp--setters