javascript - 在 Sequelize 中将 then promise 用于链式 promise 时未定义对象
问题描述
我正在使用以下代码对invoice
Sequelize 中的对象使用链式承诺。但是对于 .invoice
的第二种用法,对象是未定义的then
。
Invoice.create({
//set values for invoice object
}).then(invoice => { //update company id
//invoice belongs to a company
Company.findOne({where: {id: companyId}}).then(company => {
return invoice.setCompany(company)
})
}).then(invoice => {
console.log('Invoice is: ' + invoice)
//create child items
invoice.createItem({
//set item values
})
}).catch(err => {
console.log('Invoice create error: ' + err)
})
控制台中的输出是Invoice is :undefined
. 我在这里做错了什么?
解决方案
那是因为您需要在第一个.then
回调中返回承诺。
改变这个:
Invoice.create({
//set values for invoice object
}).then(invoice => { //update company id
//invoice belongs to a company
Company.findOne({where: {id: companyId}}).then(company => {
return invoice.setCompany(company)
})
}).then(...)
至:
Invoice.create({
//set values for invoice object
}).then(invoice => {
// return this promise
return Company.findOne({where: {id: companyId}}).then(company => {
invoice.setCompany(company)
// you also need to return your invoice object here
return invoice
})
}).then(...)
推荐阅读
- python-3.x - python中的转义序列给出错误的结果
- php - 000webhost 网站上的数据库错误编号 1231
- android - 从内部存储加载图像时如何消除滚动滞后?
- sql - dm_exec_connections 不显示某些连接
- python - 从图中获取所有双连接组件(任何大小,而不是最大子图)的算法?
- c++ - cpp 静态对象实例化
- powershell - 在 PowerShell 中清理 DistributionGroup
- javascript - SyntaxError:使用 babel 和 jest 测试反应应用程序时令牌无效或意外
- android - 升级到 Gradle 5.4.1 和 Android Stuidio 3.5 RC 3 后,crashlyticsUploadDeobsRelease 任务消失了
- html - grid-template-areas 垂直分布一组文章