【发布时间】:2015-11-04 03:05:22
【问题描述】:
我在节点中有一个中间件设置来执行任务并在成功或失败时调用 next。在初始承诺块运行后调用该任务。在 .then 函数中调用它:
var Q = require('q');
var dataPromise = getCustomerId();
dataPromise
.then(function(data) {
getGUID(req, res, next);
}, function(error) {
console.log('Failure...', error);
});
};
服务器挂起,因为 (req,res,next) 参数在 .then 函数的上下文中都是未定义的。
这里是getCustomerId函数:
var getCustomerId = function() {
var getCustomerIdOptions = {
options...
};
var deferred = Q.defer();
request(getCustomerIdOptions, function(err,resp,body){
if(err){
deferred.reject(err);
console.log(err);
return;
}else{
deferred.resolve(body);
}
});
return deferred.promise;
};
将这些参数传递给 .then 块中调用的函数的正确方法是什么?
编辑: (req,res,next) 参数来自外部函数,在 .then() 块之外调用 getGUID(req,res,next) 时可以访问。
var assureGUID = function(req, res, next) {
if(app.locals.guid){
next();
return;
}
var dataPromise = getCustomerId();
dataPromise
.then(function(data) {
getGUID(req, res, next)
}, function(error) {
console.log('Failure...', error);
}).;
};
【问题讨论】:
-
你能告诉我们这些论点是从哪里来的吗?您很可能错过了在假设提供它们的中间件框架中注册的整个包装函数。
-
我添加了具有可用参数的外部函数。
-
是的,它是一个快递应用