Sequelize: TypeError: Converting circular structure to JSON

Sequelize: TypeError: Converting circular structure to JSON

我有 OrdersDisplay 型号。协会是这样运作的

Display.associate = models => Display.hasOne(models.Orders);

orders table 有 displayId 参考,我的设计中没有任何循环。但是我在下面收到此错误。

TypeError: Converting circular structure to JSON
    --> starting at object with constructor 'Socket'
    |     property '_writableState' -> object with constructor 'WritableState'
    |     property 'afterWriteTickInfo' -> object with constructor 'Object'
    --- property 'stream' closes the circle
    at JSON.stringify (<anonymous>)

如果我不包括如下所示的 dbDisplay,错误就会消失。

export const getAllOrders = async (req, res) => {
    console.log("GET ALL ORDERS")
    let orders = await dbOrders.findAll({
        include: [{
            model: dbDisplay,
        }]
    }).catch(error => res.status(400).send(error));
    try {
        res.status(200).send(orders) // error here

    } catch (err) {
        console.log(err)
    }
}

不要尝试自己发送模型实例。使用他们的 get({ plain: true }) 方法获取普通对象:

export const getAllOrders = async (req, res) => {
    console.log("GET ALL ORDERS")
    try {
        const orders = await dbOrders.findAll({
        include: [{
            model: dbDisplay,
        }]
        })
        res.status(200).send(orders.map(x => x.get({ plain: true })))
    } catch (err) {
        console.log(err)
        res.status(400).send(err) 
    }
}