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.

Inserting field with `undefined` value inserts `null` instead of `undefined`

See original GitHub issue

For example:

collection.insert({ name: undefined })

inserts { name: {} } and not { name: undefined }. I’m using monk 3.0.4.

The mongo shell inserts { name: undefined } for same insert command.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:9

github_iconTop GitHub Comments

2reactions
ceoworkscommented, Jan 16, 2020

it seems like mongo has connection option ignoreUndefined, which could solve this issue. https://mongodb.github.io/node-mongodb-native/2.1/reference/connecting/connection-settings/

2reactions
mathieudutourcommented, Jun 21, 2018

hum maybe something like:

module.exports = function (context) {
  return function (next) {
    return function (args, method) {
      if (!args.data) {
        return next(args, method)
      }

      if (typeof args.data !== 'object') {
        return next(args, method)
      }

      const dataWithoutUndefined = Object.keys(args.data).reduce((prev, k) => {
        if (typeof args.data[k] !== 'undefined') {
          prev[k] = args.data[k]
        }
        return prev
      }, {})

      args.data = dataWithoutUndefined
      return next(args, method)
    }
  }
}
Read more comments on GitHub >

github_iconTop Results From Across the Web

Doctrine inserting NULL values for undefined fields
I suppose this is default behaviour, as it could be treated logical to create a full record in one INSERT statement.
Read more >
Unexpected NULL Fields errors when updating or inserting ...
The error statement: "Error: Wrong number of fields in row being inserted" Occurred when passing a null value during insert operations. The ...
Read more >
SQL inserting NULL values - w3resource
Inserting NULL values. The SQL INSERT statement can also be used to insert NULL value for a column. Example: Sample table: agents.
Read more >
Convert undefined to null on insert/update - MongoDB Jira
Whenever mongosh tries to insert undefined values, those undefined values get converted to null in the driver logic.
Read more >
Understanding the difference between null and undefined in ...
A property with a null value must have it explicitly assigned. Properties not defined in an item have an undefined value. In the...
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