如何将内部回调的结果传递到其父函数中?
How to pass a result from inside callback out into its parent function?
我正在使用带有 Mongoose 和 MongoDB 的 MMEAN 堆栈。我想测试集合 Foo 是否为空,但它涉及一个带有回调的 Mongoose find()
函数。 我不熟悉回调,所以我想知道如何从回调内部获取一条信息到其父函数中。
这是我必须为 addFoo 遵循的逻辑:
1.检查Foo集合是否为空
2.如果Foo为空,保存新的Foo文档
3.如果Foo不为空,则不保存新的Foo文档
我正在从 routes/index.js.
调用保存方法 addFoo()
// routes/index.js
router.post('/foo', function(req, res, next){
var foo = new Foo(req.body);
foo.addFoo(function(err, bid){
if(err){ return next(err); }
res.json(foo);
});
});
// models/Foo.js
var mongoose = require('mongoose');
var FooSchema = new mongoose.Schema({
creator: String
});
mongoose.model('Foo', FooSchema);
FooSchema.methods.addFoo = function(cb){
// finds all documents of Foo into the "results" array
this.model("Foo").find(function(err, results){
if (err){return err;}
// if the Foo results array is empty
if (results.length == 0){
// HOW DO I LET addFOO() KNOW THAT THE ARRAY IS EMPTY?
// somehow pass the boolean out to addFoo()
}
});
// IF Empty
// this.save(cb);
}
简答:你不知道。
回调在 addFoo
函数结束后执行。您基本上将其余函数放在回调中。
由于您需要访问 this
,您可以通过 var self = this;
将其绑定到一个变量,然后在回调中使用 self
。
另一种选择是使用 promises 和 denodeify
函数,将回调请求函数转换为 Promise
返回函数。
这将按如下方式工作...
FooSchema.methods.addFoo = function(cb){
var that = this;
this.model("Foo").find(function(err, results){
if (err){return err;}
// if the Foo results array is empty
if (results.length == 0){
that.save(cb);
}
});
}
但是如果你想根据你的实际需要将它传递给父级,那么你可以尝试如下....
FooSchema.methods.addFoo = function(cb){
var that = this;
this.model("Foo").find(function(err, results){
if (err){return err;}
// if the Foo results array is empty
that.commit(!results.length);//Calling a parent function from inside callback function
});
function commit(doCommit){
if(doCommit) {
that.save(cb);
}
}
}
我正在使用带有 Mongoose 和 MongoDB 的 MMEAN 堆栈。我想测试集合 Foo 是否为空,但它涉及一个带有回调的 Mongoose find()
函数。 我不熟悉回调,所以我想知道如何从回调内部获取一条信息到其父函数中。
这是我必须为 addFoo 遵循的逻辑: 1.检查Foo集合是否为空 2.如果Foo为空,保存新的Foo文档 3.如果Foo不为空,则不保存新的Foo文档
我正在从 routes/index.js.
调用保存方法addFoo()
// routes/index.js
router.post('/foo', function(req, res, next){
var foo = new Foo(req.body);
foo.addFoo(function(err, bid){
if(err){ return next(err); }
res.json(foo);
});
});
// models/Foo.js
var mongoose = require('mongoose');
var FooSchema = new mongoose.Schema({
creator: String
});
mongoose.model('Foo', FooSchema);
FooSchema.methods.addFoo = function(cb){
// finds all documents of Foo into the "results" array
this.model("Foo").find(function(err, results){
if (err){return err;}
// if the Foo results array is empty
if (results.length == 0){
// HOW DO I LET addFOO() KNOW THAT THE ARRAY IS EMPTY?
// somehow pass the boolean out to addFoo()
}
});
// IF Empty
// this.save(cb);
}
简答:你不知道。
回调在 addFoo
函数结束后执行。您基本上将其余函数放在回调中。
由于您需要访问 this
,您可以通过 var self = this;
将其绑定到一个变量,然后在回调中使用 self
。
另一种选择是使用 promises 和 denodeify
函数,将回调请求函数转换为 Promise
返回函数。
这将按如下方式工作...
FooSchema.methods.addFoo = function(cb){
var that = this;
this.model("Foo").find(function(err, results){
if (err){return err;}
// if the Foo results array is empty
if (results.length == 0){
that.save(cb);
}
});
}
但是如果你想根据你的实际需要将它传递给父级,那么你可以尝试如下....
FooSchema.methods.addFoo = function(cb){
var that = this;
this.model("Foo").find(function(err, results){
if (err){return err;}
// if the Foo results array is empty
that.commit(!results.length);//Calling a parent function from inside callback function
});
function commit(doCommit){
if(doCommit) {
that.save(cb);
}
}
}