【发布时间】:2017-10-09 09:14:55
【问题描述】:
我在Promises 上玩得很开心,但遇到了一个问题。
这是我的代码
req.checkBody(BookManager.SCHEME);
req.getValidationResult()
.then(function (result) {
if (!result.isEmpty()) {
handler.onError('Invalid payload');
return;
}
return new BookModel({
author: data.author,
name: data.name,
year: data.year
});
})
.then((book) => {
book.save();
})
.then((saved) => {
handler.onSuccess(saved);
})
.catch((error) => {
handler.onError(error.message);
});
但由于某种原因,这个 then 语句
.then((book) => {
book.save();
})
将 book 参数设置为未定义。
我做错了什么,如何将then 语句的结果传递给下一个语句。
谢谢。
【问题讨论】:
-
一开始听起来像是How do I access previous promise results in a
.then()chain? 的复制品,但实际的问题是你return;有时从之前的回调中未定义。
标签: node.js asynchronous promise arguments es6-promise