I am trying to create a route that will update a specific value in an object in a nested array. I am getting a 404 error in my node console. I am almost certain that this is not the way to do it but my code will give an idea of what I want to achieve:
router.put('/getProfile/:profile_id/addWin'), function (req, res) {
UserProfile.findOne({
UserID : req.params.profile_id //Find the correct Profile
}, function (err, profile) {
if (err)
res.send(err);
profile.Drafts.findByID({ //Find the corrct Draft in the Profile
_id : req.body.DraftID
}, function (err, draft) {
if (err)
res.send(err);
draft.Wins += 1; //Increment the wins
})
profile.save(function (err) { //Save the profile
if (err)
res.send(err);
res.json({
message : 'Win added!'
});
});
});}
this is the error in the console:
PUT /api/getProfile/575ecce6924295bc21000005/addWin 404 5.128 ms - -
The route should find the correct profile (this works in my other routes), using the profile_id
, then access the Drafts array
and find the correct Draft and increment the wins
. How do I achieve this?
In my request body I just send the DraftID and the profileID
as parameter and is stored in a UserID property in the profile object.
This is my first attempt ever at the MEAN stack
so I am not comfortable yet.
POST /api/getProfile/5...
androuter.put('/getProfile/:profile_id/addWin')
=>POST != PUT
– Axel Jun 14 at 9:29