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.

Undefined attributes are dropped when saving (POST, PUT)

See original GitHub issue

Let’s say I want to save this object:

{
  foo: 1,
  bar: "...",
  baz: undefined
}

I would expect this to get sent:

{ "foo": 1, "bar": "...", "baz": null }

Instead what gets sent is:

{ "foo": 1, "bar": "..." }

Issue Analytics

  • State:closed
  • Created 10 years ago
  • Reactions:2
  • Comments:32 (12 by maintainers)

github_iconTop GitHub Comments

1reaction
grabboucommented, Feb 24, 2015

Explanation:

JSON.stringify() converts a value to JSON notation representing it:

  • Properties of non-array objects are not guaranteed to be stringified in any particular order. Do not rely on ordering of properties within the same object within the stringification.
  • Boolean, Number, and String objects are converted to the corresponding primitive values during stringification, in accord with the traditional conversion semantics.
  • If undefined, a function, or a symbol is encountered during conversion it is either omitted (when it is found in an object) or censored to null (when it is found in an array).
  • All symbol-keyed properties will be completely ignored, even when using the replacer function.

According to JSON spec, only null values are valid to be present in JSON document, so in order to preserve them, make sure you covert them to nulls before actually passing to Restangular. This can be easily achieved in request interceptors.

As this is not always the desired behaviour, I am not sure if this should be treated as a bug/feature to implement. For example - if user executes PATCH request, he expects to send only properties that has been set by the user. Otherwise - all undefined properties will be converted to nulls and may overwrite accidentally valid properties on the server side.

Normally, I think it’s a servers job to ensure that if value is not passed in the payload of POST method, it should be nullified automatically (with Hapi.js this is pretty easy when you do Joi.number().optional().default(0)).

1reaction
mark0978commented, Aug 14, 2014

Chances are really good you copied the object with something like angular.copy. Don’t do that (for now at least). The object is connected to the methods with bind() and when that happens, well, the original element that was bound to the function/method becomes the this inside the function.

As far as I know, there has been no fix for this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Vue- Cannot read properties of undefined when dropping file
i am trying to drag and drop an mp3 ...
Read more >
After removing category attributes I get errors when ...
It looks like attributes were removed from the eav_attribute table, but Magento still stores the data of the removed attributes for some ...
Read more >
Top 10 errors from 1000+ Ruby on Rails projects (and how ...
Here are the top 10 Rails errors: · 1. ActionController::RoutingError · 2. NoMethodError: undefined method '[]' for nil:NilClass · 3.
Read more >
JSON.stringify removes undefined, how to keep it
Problem was that Final Form returns undefined for the values that have been removed by the user. As you can imagine, this was...
Read more >
Mongoose v6.8.2: API docs
This option is passed to Node.js socket#setTimeout() function after the MongoDB driver successfully completes. [options.family=0] «Number» Passed transparently ...
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