Mongoose update not working for me

笑着哭i 提交于 2019-12-11 15:04:47

问题


I have an endpoint where I'm trying to update a phone number in all "Contacts" documents after updating a user "Profile" document that shares their phone numbers.

Based on the documents, I believe I have the right structure. The "Profiles" document updates fine, but "Contacts" does not.

Here is the code:

router.route('/updatephone/:id')

// UPDATE PHONE
.put(function(req, res){
  Profile.findOne({'owner_id':req.params.id}, function(err, profile){
    if(err)
      res.send(err);

      var phone = profile.phones.id(req.body._id)

      phone.phone_number = req.body.phone_number;
      phone.phone_type = req.body.phone_type;

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

        console.log('Phone ID: ' + req.body._id);
        console.log('Phone Number: ' + req.body.phone_number);
        console.log('Phone_Type: ' + req.body.phone_type);

        Contacts.update(
          {'shared.phones._id':req.body._id}, 
          {$set:{
            'shared.phones.$.phone_number':req.body.phone_number,
            'shared.phones.$.phone_type': req.body.phone_type
          }}, 
          {'multi':true})

      })
  });
});

Here is an example of the document structure for "Contacts".

{
    "_id" : ObjectId("59c3dac6764e8d7135e744e3"),
    "shared" : {
        "_id" : ObjectId("59c3dac6764e8d7135e744e5"),
        "businesses" : [ ],
        "addresses" : [ ],
        "phones" : [
            {
                "phone_number" : "5555555555",
                "phone_type" : "mobile",
                "_id" : ObjectId("59c200829aa4486971926ce9")
            },
            {
                "phone_number" : "4444444444",
                "phone_type" : "business",
                "_id" : ObjectId("59c200ad9aa4486971926cea")
            }
        ],
        "emails" : [ ],
        "first_name" : "Joe",
        "invite_id" : "PaulSmith59c1ff9f9aa4486971926ce7",
        "last_name" : "Public",
        "link" : "PaulSmith59c1ff9f9aa4486971926ce7"
    },
    "last_name" : "Smith",
    "first_name" : "Paul",
    "owner_id" : "59c1ff9f9aa4486971926ce7",
    "emails" : [
        {
            "email_address" : "paul@smith.com",
            "email_type" : "home",
            "_id" : ObjectId("59c3dac6764e8d7135e744e4")
        }
    ],
    "__v" : 1,
    "addresses" : [ ],
    "businesses" : [ ],
    "phones" : [ ]
}

回答1:


You have passed a callback function to profile.save(). Therefore, it won't return a promise so your then() method won't fire.

Try performing the Contact.update() in the profile.save() callback:

profile.save(function(err) {
  if (err) res.send(err)

  console.log('Phone ID: ' + req.body._id)
  console.log('Phone Number: ' + req.body.phone_number)
  console.log('Phone_Type: ' + req.body.phone_type)

  Contacts.update(
    {
      'shared.phones._id': req.body._id
    }, 
    {
      $set: {
        'shared.phones.$.phone_number': req.body.phone_number,
        'shared.phones.$.phone_type': req.body.phone_type
      }
    }, 
    {
      'multi': true
    },
    function(err) {
      res.json(profile)
    }
  )

})

You could do it with promises too:

profile.save()
  .then(function() {
    console.log('Phone ID: ' + req.body._id)
    console.log('Phone Number: ' + req.body.phone_number)
    console.log('Phone_Type: ' + req.body.phone_type)

    return Contacts.update(
      {
        'shared.phones._id': req.body._id
      }, 
      {
        $set: {
          'shared.phones.$.phone_number': req.body.phone_number,
          'shared.phones.$.phone_type': req.body.phone_type
        }
      }, 
      {
        'multi': true
      }
    )
  })
  .then(function() {
    res.json(profile)
  })
  .catch(function(err) {
    res.send(err)
  })


来源:https://stackoverflow.com/questions/46348270/mongoose-update-not-working-for-me

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!