My requirement is that I want to be able to send an object like {title: "test", price: undefined}
from my angular-app via $http.post to my nodejs app (to be able to delete the price key via Mongoose update in my mongoDB).
Problem:
Code extract
console.log(object)
$http.post(url, object)
- In the console, I see:
{title: "test", price: undefined}
- My request payload contains
{title: "test"}
==> Why doesn't my payload contain the full object? How can I add the price: undefined in my payload?
When HTTP request
Content-Type
isapplication/json
and the payload object containsundefined
as some fields' value, those fields will be removed. Otherwise, server side program won't be able to parse the JSON successfully -- an errorSyntaxError: Unexpected token u in JSON at position ...
will happen (the tokenu
is just the "u" of "undefined").This is not an Angular specific behaviour, all HTTP requests should have this logic.
What happens in Angular's
$http.post
?In Angular, all request data will be transformed. When the data is JSON,
toJSON
(which equals toangular.toJson
) will be invoked to do the job (source code):The
toJson()
function is just a proxy toJSON.stringify
(source code):In JSON.stringify:
It doesn't contain it because it is
undefined
. Undefined vars are removed from the request. You will just have to know, server-side that if the variable is not included, that you should delete it. Typically you want to use thePUT
verb for updating a record, vsPOST
for creating a new record.You could send an empty string or a boolean, or something special, so that you will know in your node app to delete the key.