最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

[在将Sequelize中的诺言用于随后的诺言时未定义的对象

网站源码admin16浏览0评论

[在将Sequelize中的诺言用于随后的诺言时未定义的对象

[在将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(...)
发布评论

评论列表(0)

  1. 暂无评论