Currently, I have an Express JS application where I am attempting to update an existing user's name using the PUT method. The schema I am working with is a nested array and is structured like this:
https://i.sstatic.net/WDIse.png
The code snippet I am using to update the user's name using PUT is as follows:
router.route('/sensors_update/:_id/')
.put(function (req, res) {
console.log("inside update put")
User.findById(req.params._id, function(err, user) {
if (err)
res.send(err);
console.log(user);
user.name = req.body.name;
console.log("User val is" + user);
user.save();
});
});
While I can see the user's name successfully updating in the JavaScript console, I am encountering issues when it comes to saving the user object. I suspect this may be due to the nested schema. Is my assumption correct? Any advice on how to save a user object with nested arrays would be appreciated.
Upon inspecting the console messages:
inside update put
{ _id: 'Manasa',
name: 'Manasa Sub',
__v: 0,
sensors:
[ { sensor_name: 'ras',
_id: 57da0a4bf3884d1fb2234c74,
measurements: [Object] } ] }
User val is{ _id: 'Manasa',
name: 'Manas Sub',
__v: 0,
sensors:
[ { sensor_name: 'ras',
_id: 57da0a4bf3884d1fb2234c74,
measurements: [Object] } ] }
I am puzzled as to why the nested array is being accessed when my intention is to only update and save the user's name.
Thank you
UPDATE
_http_outgoing.js:335 throw new Error('Can\'t set headers after they are sent.'); ^ Error: Can't set headers after they are sent.
How can I resolve this issue?
Response :
{
"message": "Cast to date failed for value \"7:00\" at path \"time\"",
"name": "CastError",
"type": "date",
"value": "7:00",
"path": "time"
}