0

I've seen a lot of examples, how to update an array, like this one Mongoose find/update subdocument and this one Partial update of a subdocument with nodejs/mongoose

but my objective is to update a specific field, for example in a Form, where user enters data.

I'm using ejs for templating.

Here's the code

For clarity , here is User's Schema

var UserSchema = mongoose.Schema({
   resume: {
     education: [{ type: String}],
   }

});

Routing code

router.post('/update-resume', function(req, res) {
    User.findById(req.user._id, function(err, foundUser) {

        // Take a look over here, how do I update an item in this array?
        if (req.body.education) foundUser.resume.education.push(req.body.education);

        foundUser.save(function(err) {
            if (err) return next(err);
            req.flash('message', 'Successfully update a resume');
            return res.redirect('/update-resume')
        });
    });

});

If you take a look at the code above, my objective is to query a user data which is resume, and update its current value.

Code for the front end

<form role="form" method="post">
  <div class="form-group">
    <label for="education">Education:</label>
    <% for(var i = 0; i < user.resume.education.length; i++) { %>
    <input type="text" class="form-control" name="education" id="education" value="<%= user.resume.education[i] %>">
    <% } %>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

Frontend code does work, it iterate in each user.resume.education array, and show all the values in the array. But how do I update it?

Community
  • 1
  • 1
Jack Moscovi
  • 2,195
  • 7
  • 30
  • 49

1 Answers1

3

Since you're using .push() to the education array, mongoose don't know that this field has changed, you need to specify it with the markModified() function so mongoose will know that this field has changed, so, after you push to the education array use:

foundUser.markModified('resume.education');

and then use the save() function

UPDATE:

router.post('/update-resume', function(req, res) {
    User.findById(req.user._id, function(err, foundUser) {

        // Take a look over here, how do I update an item in this array?
        if (req.body.education) foundUser.resume.education.push(req.body.education);

        foundUser.markModified('resume.education'); // <-- ADDITION

        foundUser.save(function(err) {
            if (err) return next(err);
            req.flash('message', 'Successfully update a resume');
            return res.redirect('/update-resume')
        });
    });

});
udidu
  • 8,269
  • 6
  • 48
  • 68
  • Check out the gist, for somehow, I don't know how to implement this. – Jack Moscovi Oct 25 '15 at 08:42
  • and second how does body parser will know which req.body belong to which index? – Jack Moscovi Oct 25 '15 at 08:42
  • you need to use the same code you showed in your question, and just add the markModified row between the `push` and the `save` actions – udidu Oct 25 '15 at 08:46
  • Just to let you know, I did what you did, and it keeps pushing a new value to the array instead of updating it. Does it have anything to do with the frontend or backend? – Jack Moscovi Oct 25 '15 at 08:51
  • of course it will, you have a String array, how you think it will update the value? each element inside your education array is just a string, not an object with an ID, my suggestion, read about the population here: http://mongoosejs.com/docs/populate.html, this is what I think you meant – udidu Oct 25 '15 at 08:54