Dismiss
Announcing Stack Overflow Documentation

We started with Q&A. Technical documentation is next, and we need your help.

Whether you're a beginner or an experienced developer, you can contribute.

Sign up and start helping → Learn more about Documentation →

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.

share|improve this question
1  
Just a tip: POST /api/getProfile/5... and router.put('/getProfile/:profile_id/addWin') => POST != PUT – Axel Jun 14 at 9:29
    
Thanks you're right, I copied the wrong message as I was playing around with put/post. Edited :) – Poot87 Jun 14 at 9:39
up vote 0 down vote accepted

you must pass the callback function(req,res) as second parameter in the router like this: (no closing th function after the url)

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!'
        });
    });
  });
});
share|improve this answer
    
Thanks that worked! However, now I have a whole new world of problems haha. I think they might be for a different question. – Poot87 Jun 15 at 6:57

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.