I have Orders
and Display
models. The association works like this
Display.associate = models => Display.hasOne(models.Orders);
The orders
table has displayId
reference and there is nothing circular in my design. However I get this error below.
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>)
the error dissapears if I do not include dbDisplay
shown below.
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)
}
}
CodePudding user response:
Don't try to send model instances themselves. Use their get({ plain: true })
method to get plain object(s):
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)
}
}