Can't update mongo array using RESTful api put

I am very new to MEAN stack and recently finished a tutorial and am trying to develop it. It is a simple application that uses RESTful api to update mongo db. I am now using the put statement to update some parts of my documents in my collections. The only problem is when I try to update the array using this put statement, I get a 500 error. This is the put statement in the Server.js file :

app.put('/users/:id', function (req, res) {
      var id = req.params.id;
      console.log(req.body.name);
      db.users.findAndModify({
        query: {_id: mongojs.ObjectId(id)},
        update: {$set: {name: req.body.help.name, email: req.body.email, number:                    req.body.number}},
        new: true}, function (err, doc) {
      res.json(doc);
    }    
  );
});

      

If you notice the update statements body.email

and body.number

are working fine. However, I tried to make a very simple array just to test the theme. In one of my docs, I have this array.

"help" : [ 
        {
            "name" : "test"
        }
    ],

      

All I would like to do is figure out how to change the value in the array. The put statement works fine for modifying strings in a mongo document, but not for an array. I noticed that req.body.string

was how you were accessing it, so I figured that all I had to do was add to it, so in this case req.body.help.name

. However this doesn't work, all I get is a 500 error in my console log. Any help would be appreciated!

+3


source to share


1 answer


So the answer turned out to be that I didn't use an array in my $ set for the findandmodify update part. This is what the code should look like: update: {$ set: {help: {name: req.body.help.name}}}



0


source







All Articles