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

编程入门 行业动态 更新时间:2024-10-11 07:32:41

[在将Sequelize中的<a href=https://www.elefans.com/category/jswz/34/1756615.html style=诺言用于随后的诺言时未定义的对象"/>

[在将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中的诺言用于随后的诺言时未定义的对象

本文发布于:2024-05-07 07:00:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1754979.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:诺言   对象   未定义   Sequelize

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!