0

I am trying to update object values inside of array. I have done the method and search the element in the array that I want, and returns me 200. But after when I do another request the value return to the original (is not saving).

First of all this is the schema:

    { 
         "_id" : "1",
         "username" : "a",
         "elements" : [{"_id": "22", "name":"bb"}, {"_id":"33", "name": "cc"}]   
    }

and this is my method

update = function(req, res) {
        User.findById(req.params.id, function (err, user) {
        if (!user) {
            res.send(404, 'User not found');
        }
        else{
            var array = user.elements;
            for (var i = 0; i < array.length; i++) { 
                if (array[i]._id == "22") { 
                  result = array[i];
                  if (req.body.name != null) result.name = req.body.name;
                  result.save(function(err) {
                      if(!err) {
                          console.log('Updated');
                      } 
                      else {
                          console.log('ERROR: ' + err);
                      }
                      res.send(result);
                  });
                  break;
                } 
            }
        }
        });
    }

I don't know what I am doing wrong. I mean I simplified everything but I think that the problem is in the method.

sheplu
  • 2,937
  • 3
  • 24
  • 21
david raja
  • 87
  • 11
  • You should only execute `res.send(result)` once. Use [Promise.all](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all) or [async.js](http://caolan.github.io/async/) particularly [async.mapSeries](http://caolan.github.io/async/docs.html#mapSeries) to update all the elements first THEN execute `res.send()` once. – Mikey Oct 16 '17 at 16:01
  • You should also edit your question with your schema(s). – Mikey Oct 16 '17 at 16:05
  • I believe res.send is only called once @Mikey -- although hard to tell with the indenting. – Mark Oct 16 '17 at 16:06
  • did you console req.params.id if it has something? did you try to console out error after find? Where does your code stop? what console did you print so far? – Theo Oct 16 '17 at 16:06
  • Are you sure you don't want `user.save()`. – Mark Oct 16 '17 at 16:08
  • you hard coded the `_id` check. Do you still want it hardcoded instead of `if (array[i]._id == req.params.id) { ` ? – Andrew Lohr Oct 16 '17 at 16:10
  • @Mark_M You're probably right but it's hard to tell as his `res.send()` is within a loop. There's an easier way to [find a subdocument by its ID](https://stackoverflow.com/a/20637479/1022914) though. – Mikey Oct 16 '17 at 16:11
  • Thanks guys!! the problem was with the res.send(result), now i doing an user.save() and sending the results after and it's working. thanks again!! – david raja Oct 16 '17 at 16:13

1 Answers1

1

You have to save the user object and result just like this :

  update = function(req, res) {
            User.findById(req.params.id, function (err, user) {
            if (!user) {
                res.send(404, 'User not found');
            }
            else{
                var array = user.elements;
                for (var i = 0; i < array.length; i++) { 
                    if (array[i]._id == "22") { 
                      result = array[i];
                      if (req.body.name != null) result.name = req.body.name;

                      break;
                    } 
                }
                 user.save(function(err) {
                          if(!err) {
                              console.log('Updated');
                          } 
                          else {
                              console.log('ERROR: ' + err);
                          }
                          res.send(user);
                      });
            }
            });

        }
El houcine bougarfaoui
  • 35,805
  • 9
  • 37
  • 37