Reputation: 1792
the delete route:
router.delete('/users/:name' , function (req, res, next) {
User.deleteOne({name: req.params.name}).then (function (user) {
console.log('DELETED / ', req.params.name);
res.send('DELETED / ', req.params.name);
}).catch (next)
});
the router.get
and router.post
under the same '/users/' work no problem.
I get a strange error when I try this,
{
"error": "Unexpected token n in JSON at position 3"
}
although I have a 200 OK status response. Any idea what is going on? I'm trying in postman.
UPDATE:
Lesson learned here. Make sure your testing methods are in fact correct.
I was sending a different header that somehow got mixed up in postman which causes errors. It was a hard to notice at first but clicking the setting I discovered there as a strange extra huge JSON batch being sent back. Even though at first glance everything seemed ok
Upvotes: 2
Views: 116
Reputation: 26
Based on the Express API reference of res.send():
When the parameter is an Array or Object, Express responds with the JSON representation
Your code above seems to send the string "DELETED / "
back. Maybe that is the reason why your JS code raise JSON parse error.
Try replace res.send
statement with this one below:
res.send({msg : 'DELETED / ', user: req.params.name});
Hope this helps.
Upvotes: 1
Reputation: 5202
Try like this :
router.delete('/users/:name' , function (req, res) {
User.deleteOne({name: req.params.name})
.exec()
.catch (err => res.status(500).send(err) )
.then (function () {
console.log('DELETED / ', req.params.name);
res.send('DELETED / ', req.params.name);
})
});
Upvotes: 2