诺言用于随后的诺言时未定义的对象"/>
[在将Sequelize中的诺言用于随后的诺言时未定义的对象
我正在使用以下代码对Sequelize中的invoice
对象使用链式诺言。但是对于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)
})
})
收件人:
Invoice.create({
//set values for invoice object
}).then(invoice => { //update company id
//invoice belongs to a company
// return this promise
return Company.findOne({where: {id: companyId}}).then(company => {
invoice.setCompany(company)
return invoice
})
}).then(...)
更多推荐
[在将Sequelize中的诺言用于随后的诺言时未定义的对象
发布评论