是否可以将额外的参数传递给猫鼬更新回调 [英] Is it possible to pass an extra parameter to a mongoose update callback

查看:81
本文介绍了是否可以将额外的参数传递给猫鼬更新回调的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有猫鼬更新呼叫,想传递一个额外的参数...就像这样:

I have mongoose update call and would like to pass an extra parameter...like so:

试图通过 isLoopOver

UserInfo.update({_id: userInfo._id}, {'value': someval}, function(err, numAffected, isLoopOver ) {
    console.log('IsLoopOver ' + JSON.stringify(isLoopOver) );
    if (isLoopOver){
        doSomething(isLoopOver);
    }
});

尝试了上述操作,但在回调中得到了一个对象,如下所示:

Tried the above but I get an object (inside the callback) as below:

{"updatedExisting":true,"n":1,"connectionId":117,"err":null,"ok":1}

不知道为什么它显示了mongo的状态.

No idea why it is showing the status from mongo.

问题:如何通过回调传递一个额外的参数?

Question: How can I pass an extra parameter thru the callback?

推荐答案

常见方法是:

var isLoopOver = false;
UserInfo.update({_id: userInfo._id}, {'value': someval}, function(err, numAffected) {
    console.log('IsLoopOver ' + JSON.stringify(isLoopOver) );
    if (isLoopOver){
        doSomething(isLoopOver);
    }
});

如果您担心某些代码会在调用update的回调函数之前更改isLoopOver的值,请使用以下代码:

If you are worried about some code will change the value of isLoopOver before the update's callback function being called, using the following code:

(function (isLoopOver) {
  UserInfo.update({_id: userInfo._id}, {'value': someval}, function(err, numAffected) {
      console.log('IsLoopOver ' + JSON.stringify(isLoopOver) );
      if (isLoopOver){
          doSomething(isLoopOver);
      }
  });
}(isLoopOver));

isLoopOver变量显示mongo状态的原因是在回调函数中,isLoopOver是形式参数而不是实际参数.

The reason why your isLoopOver variable is showing the status from mongo is that in the callback function, the isLoopOver is a formal parameter instead of a actual parameter.

这篇关于是否可以将额外的参数传递给猫鼬更新回调的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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