question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Is there any way to omit null values?

See original GitHub issue

What 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:

  1. Define models X, Y, …
  2. Run the following
  3. 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:open
  • Created 4 years ago
  • Reactions:3
  • Comments:9 (6 by maintainers)

github_iconTop GitHub Comments

34reactions
jingsamcommented, May 31, 2019

@knoxcard I found a workaround that using JSON.stringify(value, replacer):

JSON.stringify(scene, (k, v) => (v === null ? undefined : v))

if you are using express, you can do like this:

app.set('json replacer', (k, v) => (v === null ? undefined : v))
11reactions
knoxcardcommented, May 29, 2019

Setup getters in your model…

const Scene = sequelize.define(
  'scene',
  {
    sceneId: { type: Sequelize.STRING, allowNull: false, primaryKey: true },
    owner: { type: Sequelize.STRING, allowNull: false },
    name: {
      type: Sequelize.STRING
      get() {
        if(!this.getDataValue('name'))
          return
        return this.getDataValue('name')
      }
    }
  }
)

http://docs.sequelizejs.com/manual/models-definition.html#getters--amp--setters

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found