我有一个对象Word,它是回送(http://docs.strongloop.com/display/DOC/Model#Model-Model.count([query],callback))的模型。它具有一组用于处理信息的接口。即它有两种方法Word.count()和Word.find()

两种方法都通过回调获取数据。我需要再问一种方法。

this.count({}, function (err, count) {
    if(err) {
        fn(err);
    }
    else {
               //here i want to call smth like
               //this.find({},function(err,result){..})
               //but can`t, cause "this" is undefine
    }
});


如何制作链条?

最佳答案

this是js问题的常见原因。一种有用的技术是将其设置为其他变量,以便您可以在闭包中使用它,就像您想要的那样。

这样行吗?

self=this;
this.count({}, function (err, count) {
    if(err) {
        fn(err);
    }
    else {
        self.find(...);
               //here i want to call smth like
               //this.find({},function(err,result){..})
               //but can`t, cause "this" is undefine
    }
});

09-16 18:30