Undefined attributes are dropped when saving (POST, PUT)
See original GitHub issueLet’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:
- Created 10 years ago
- Reactions:2
- Comments:32 (12 by maintainers)
Top 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 >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 FreeTop 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
Top GitHub Comments
Explanation:
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)
).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.