0

I'm trying to update all documents that match the following criteria.

For some reason, I'm getting an error in the console saying that alerts.save is not a function.

This is how I've been updating my records.

Is the issue that I'm trying to update more than one document?

router.route('/mark_as_read/:ownerId')
.put(function(req, res){

  var alerts = new Alerts();

  Alerts.find({owner_id: req.params.ownerId}, function(err, alerts){
    if(err)
        res.send(err);

    alerts.viewed = true;
    console.log('Alerts: ', alerts);

    alerts.save(function(err){
      if(err)
        res.send(err);
      res.json(alerts)
    });
  });
});

Here is an example of the data returned when I do a get:

  { _id: 59aed31e78fbbea611518ddf,
    message: 'Hammer Hammer at hammer@example.com',
    alert_type: 'Share',
    owner_id: '59a6e66d5ab67150837d96a8',
    __v: 0,
    viewed: false,
    date: 2017-09-05T16:38:54.437Z },
  { _id: 59aed6035fee3fa895045693,
    message: 'Paul Pataa at paul@example.com',
    alert_type: 'Share',
    owner_id: '59a6e66d5ab67150837d96a8',
    __v: 0,
    viewed: false,
    date: 2017-09-05T16:51:15.193Z },
  { _id: 59aedba6fec346aafae9121c,
    message: 'Marky Mark at marks@example.com',
    alert_type: 'Share',
    owner_id: '59a6e66d5ab67150837d96a8',
    __v: 0,
    viewed: false,
    date: 2017-09-05T17:15:18.876Z },
  { _id: 59aedc36fec346aafae91228,
    message: 'Lilly Lap at sammy@example.com',
    alert_type: 'Share',
    owner_id: '59a6e66d5ab67150837d96a8',
    __v: 0,
    viewed: false,
    date: 2017-09-05T17:17:42.951Z }

I tried iterating over the alerts array, but this dis not work for me:

router.route('/mark_as_read/:ownerId')
.put(function(req, res){

  var alerts = new Alerts();

  Alerts.find({owner_id: req.params.ownerId}, function(err, alerts){
    if(err)
        res.send(err);

    for(var i=0; i < alerts.length; i++){
      alerts.viewed = true;
      alerts[i].save(function(err){
        if(err)
          console.log('Error in updating an alert');

      })
    }
  });
});
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
cnak2
  • 1,711
  • 3
  • 28
  • 53

1 Answers1

1

The find method will return an array so alerts.save() won't work.

You would have to iterate over the array and call save() on each document in the array.

Alternatively, you could use findOne instead, which will return a single document.

Steve Holgado
  • 11,508
  • 3
  • 24
  • 32
  • Thanks Steve. Makes sense. I updated my example with a loop over the alerts array, but this didn't work. Anywhere I can see a clear example of handling this? Thanks for your help!! – cnak2 Sep 08 '17 at 23:33
  • On this line **alerts.viewed = true** you are trying to reference a **viewed** property of the array itself. You might experience issues with doing it this way as the **save** calls will be async but the for loop will be sync. Therefore, i'd recommend using **findOne** or **findById** as you are only matching a single document anyway. – Steve Holgado Sep 08 '17 at 23:54
  • I'm actually trying to update all documents that have the same owner_id. So using findOne or findById won't work in my case. There must be a simple way to update multiple documents. I found how to do it in mongo using the .update method, but that doesn't work in my express app. Have you ever searched through a collections and change a single field in each document returned? That's my challenge. Thanks again. I'm sure the answer is right in front of me. I'm just not getting it yet. :-) – cnak2 Sep 09 '17 at 00:22
  • Give this a try using the **multi** option: **Alerts.update({owner_id: req.params.ownerId}, {viewed: true}, {multi: true}, function(err, numAffected) {...});** So the signature is: **Model.update(conditions, update, options, callback);** – Steve Holgado Sep 09 '17 at 09:26
  • Thanks, Steve. I actually found that in the docs and it worked. Appreciate your help. – cnak2 Sep 10 '17 at 14:33
  • No problem. Glad you got it working. – Steve Holgado Sep 10 '17 at 17:15