Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
118
rated 0 times [  125] [ 7]  / answers: 1 / hits: 26319  / 7 Years ago, sun, february 26, 2017, 12:00:00

I have a node object that looks like this:



{
_id:58b336e105ac8eec70aef159,
name:my node,
ip:192.168.1.1,
__v:0,
configuration:{
links:[
{
linkName:athena_fw_listen_tcp_5555,
_id:58b336e105ac8eec70aef15d,
local:true
},
{
linkName:athena_fw_listen_udp_5554,
_id:58b336e105ac8eec70aef15c,
local:true
}
]
}
}


I am sending a delete request to my express server that looks like this:
DELETE http://localhost:9000/api/nodes/58b336e105ac8eec70aef159/links/58b336e105ac8eec70aef15d



I followed instructions in $pull mongodb documentation and I also tried this



But it does not seem to work, as I keep receiving: 500 (Internal Server Error)



This is how the code on my express side looks like:





exports.destroyLink = function(req, res) {
Node.findById(req.params.id, function(err, node) {
if (err) { return handleError(res, err); }
if (!node) { return res.status(404).send('Not Found'); }
console.log(Node, JSON.stringify(node))
console.log(Params, req.params)

node
.update({ '_id': req.params.id }, { $pull: { 'configuration': { 'links': { '_id': req.params.linkId } } } }, false, true)
.then(err => {
if (err) { return handleError(res, err); }
return res.status(204).send('No Content');
});
})
};





The express router contains this:
router.delete('/:id/links/:linkId', controller.destroyLink);



So I am expecting id and linkId as params, I use id (_id: req.params.id) to target a specific node and linkId (_id: req.params.linkId) to target a specific link, but it doesn't work!



Need help resolving the issue, I don't know what I am missing here!


More From » node.js

 Answers
21

Hi all and thank you for your help. I finally got it to work!!!



After almost 3 hours of troubleshooting :'( :'( this is the solution that used:



exports.destroyLink = function(req, res) {
Node.findByIdAndUpdate(
req.params.id, { $pull: { configuration.links: { _id: req.params.linkId } } }, { safe: true, upsert: true },
function(err, node) {
if (err) { return handleError(res, err); }
return res.status(200).json(node.configuration.links);
});
};


Instead of findById then update the node, I did both using findByIdAndUpdate. It' working perfectly now!!!



I still don't find an explanation for the other version though. But anyways glad it's working this way.


[#58766] Friday, February 24, 2017, 7 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
tobyl

Total Points: 598
Total Questions: 110
Total Answers: 114

Location: Vietnam
Member since Sat, Feb 12, 2022
2 Years ago
tobyl questions
Tue, Aug 10, 21, 00:00, 3 Years ago
Wed, Jan 13, 21, 00:00, 3 Years ago
Tue, Dec 1, 20, 00:00, 4 Years ago
;