对象不是 Return 来自 Promise 函数
Object Not Return From Promise Function
// Thing is a mongoose model imported from thing.model.js file (the mongoose in that file has been promisify)
exports.show = function(req, res) {
return res.json(getThing(req.params.id))
};
function getThing(thingID){
return Thing.findByIdAsync(thingID).then(function(thing){
return thing;
})
}
如何从函数中取出东西。现在,它只是 returns 一个承诺对象(解决和拒绝字段)。如果我删除 getThing 辅助函数中的第一个 'return',它将 returns 什么都没有。 (我试过 console.log(then 回调块中的东西,它工作正常))
如果我这样写:
exports.show = function(req, res) {
return Thing.findByIdAsync(req.params.id)
.then(function(thing){return res.json(thing);})
};
会成功的!为什么?
这是您的最佳摘要
exports.show = function(req, res) {
return res.json(getThing(req.params.id))
};
function getThing(thingID){
return Thing.findByIdAsync(thingID).then(function(thing){
return thing;
})
}
在 getThing
中 .then 是多余的,所以 getThing
本质上是
function getThing(thingID){
return Thing.findByIdAsync(thingID);
}
所以exports.show基本上是
exports.show = function(req, res) {
return res.json(Thing.findByIdAsync(req.params.id))
};
你实际上是在 res.json 承诺
不同于:
exports.show = function(req, res) {
return Thing.findByIdAsync(req.params.id)
.then(function(thing){return res.json(thing);})
};
你要返回 findByIdAsync
结果的 res.json 的承诺
如果你想拆分功能你想做什么
exports.show = function(req, res) {
return getThing(req.params.id)
.then(function(thing){return res.json(thing);})
};
function getThing(thingID){
return Thing.findByIdAsync(thingID);
}
或
exports.show = function(req, res) {
return getThing(req.params.id);
};
function getThing(thingID){
return Thing.findByIdAsync(thingID)
.then(function(thing){return res.json(thing);});
}
// Thing is a mongoose model imported from thing.model.js file (the mongoose in that file has been promisify)
exports.show = function(req, res) {
return res.json(getThing(req.params.id))
};
function getThing(thingID){
return Thing.findByIdAsync(thingID).then(function(thing){
return thing;
})
}
如何从函数中取出东西。现在,它只是 returns 一个承诺对象(解决和拒绝字段)。如果我删除 getThing 辅助函数中的第一个 'return',它将 returns 什么都没有。 (我试过 console.log(then 回调块中的东西,它工作正常))
如果我这样写:
exports.show = function(req, res) {
return Thing.findByIdAsync(req.params.id)
.then(function(thing){return res.json(thing);})
};
会成功的!为什么?
这是您的最佳摘要
exports.show = function(req, res) {
return res.json(getThing(req.params.id))
};
function getThing(thingID){
return Thing.findByIdAsync(thingID).then(function(thing){
return thing;
})
}
在 getThing
中 .then 是多余的,所以 getThing
本质上是
function getThing(thingID){
return Thing.findByIdAsync(thingID);
}
所以exports.show基本上是
exports.show = function(req, res) {
return res.json(Thing.findByIdAsync(req.params.id))
};
你实际上是在 res.json 承诺
不同于:
exports.show = function(req, res) {
return Thing.findByIdAsync(req.params.id)
.then(function(thing){return res.json(thing);})
};
你要返回 findByIdAsync
如果你想拆分功能你想做什么
exports.show = function(req, res) {
return getThing(req.params.id)
.then(function(thing){return res.json(thing);})
};
function getThing(thingID){
return Thing.findByIdAsync(thingID);
}
或
exports.show = function(req, res) {
return getThing(req.params.id);
};
function getThing(thingID){
return Thing.findByIdAsync(thingID)
.then(function(thing){return res.json(thing);});
}