猫鼬更新不适用于我 [英] Mongoose update not working for me

查看:121
本文介绍了猫鼬更新不适用于我的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个端点,在我要更新所有共享其电话号码的用户个人资料"文档后,尝试更新所有联系人"文档中的电话号码.

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.

这是代码:

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" : [ ]
}

推荐答案

您已将回调函数传递给 profile.save().因此,它不会返回承诺,因此您的 then()方法不会触发.

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

尝试在 profile.save()回调中执行 Contact.update():

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)
    }
  )

})

您也可以兑现承诺:

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)
  })

这篇关于猫鼬更新不适用于我的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆