0

My data is as following:

{
"profileID": "123",
"fullname": "Name",
"notifications": [
  {
    "read": false,
    "date": "2017-05-06 13:40:01",
    "post": "5555555",
    "action": "commented",
    "profileID": "456"
  },
  {
    "read": false,
    "date": "2017-05-06 13:40:15",
    "post": "5555555",
    "action": "commented",
    "profileID": "456"
  }
]}

I am trying to make a Node API route to be able to update each notification. For the uniqueness, the date variable can be used.

So to summarise:

  • Find user by profileID
  • Get his/her notifications
  • Update the read value for the notification that matches the date.

I have constructed it in the following way:

apiRouter.post('/api/changeNotificationStatus', function(req, res){

        userModel.update(
            {profileID: req.body. profileID, "notifications.date": req.body.date, "notifications.post": req.body.post, "notifications.action": req.body.action},
            {$set:{"notifications.$.read": true}},
            {multi: false},

            function(err, data) {
                if (err){
                    console.log(err);
                } else {
                    console.log(data);
                }
            });

    });

There aren't any errors but I get the following:

{ n: 0, nModified: 0, ok: 1 }

I have confirmed that the variables: req.body.profileID, req.body.date, req.body.date, req.body.post and req.body.action are coming through just fine.

Am I doing something wrong?

PS: Thanks Neil Lunn for pointing me to the model.update post!

Sean
  • 1,151
  • 3
  • 15
  • 37
  • @neil-lunn - I have updated the question so it isn't a duplicate anymore. – Sean Jun 03 '17 at 14:06
  • Still a duplicate. Your syntax is wrong. Read the answer again. Here's the correction. `{profileID: req.body.profileID, "notifications.date": req.body.date, "notifications.post": req.body.post, "notifications.action": req.body.action}`. So you don't use the `$` in the "query" portion of the update. You only use it in the part after `$set`. – Neil Lunn Jun 03 '17 at 14:10
  • Thanks for pointing it out and sorry for the silly mistakes. However, I just tried it and am getting the same result, i.e.: { n: 0, nModified: 0, ok: 1 }... I checked the params again (via a debugger) and they seem to be just fine. Any ideas? – Sean Jun 03 '17 at 14:18
  • And I guess this question is not a duplicate anymore – Sean Jun 04 '17 at 03:51
  • The `n:0` indicates the query of your `update` didn't find a document to update. – JohnnyHK Jun 04 '17 at 04:40
  • Can you update your question to include the `req.body` contents that you're expecting to update the sample document (but isn't)? – JohnnyHK Jun 04 '17 at 05:31
  • Hey, sorry I deleted my comment before this. And the problem has been found - it was the date format..... It slipped right between the cracks a million times. Thanks for pointing it out mate – Sean Jun 04 '17 at 05:33

1 Answers1

0

Just to help anyone who comes across this, there were multiple problems:

1) The schema for userModel was wrong, as in, it should have been:

notifications:   [{
   read: Boolean,
   date: Date,
   post: String,
   action: String,
   profileID: String
}]

Instead of:

notifications:   [{ }]

2) In the userModel.update command, I should've done:

new Date(req.body.date)

Instead of:

req.body.date

Hope this helps someone in the future.

Sean
  • 1,151
  • 3
  • 15
  • 37