Skip to content Skip to sidebar Skip to footer

Node JS How To Update Inner-inner Elements

I'm working with mongo and nodejs and I'm trying to add a response object to a question object Here's the part of the model I'm working with: questions: [ { name: Stri

Solution 1:

When you need to add an object inside of array, you should use the $push function, because you have a subdocument inside your question model. In your case, inner-inner, probably you can do this:

Survey.findById(survey._id, function(error, res) {
    for(var i = 0; i < req.body.response.length && i < survey.questions.length; i++) {
        var response = req.body.response[i];
        if(response.trim() == "") continue;
        (res.questions[i]).responses.push(response);
    }
    res.save(function(error, res) {
        //  survey updated with responses.
    }
}

Let me know if this works.


Post a Comment for "Node JS How To Update Inner-inner Elements"