javascript - express 和sequelize 将循环结构转换为JSON

标签 javascript node.js express sequelize.js

我有一个异步 Sequelize 函数

async getTrips() {
    let trips = await Trip.findAll({
        order: [['id']]
    });

    const data  = trips.map(trip => ({
        ...trip,
        milestones: async () => await Milestone.findAll({
            where: {
                trips_id: trip.id
            }
        }),
        vendor_charges: async () => await VendorCharge.findAll({
            where: {
                trips_id: trip.id
            }
        }),
        trip_notes: async () => await TripNote.findAll({
            where: {
                trips_id: trip.id
            }
        }),
        pieces: async () => await Pieces.findAll({
            where: {
                trips_id: trip.id
            }
        })
    }))
    return data
}

然后在快速路由器中运行

tripsRouter.get('/getAllTrips', (req, res) => {
    const errors = validationResult(req)
    if (!errors.isEmpty())
        return res.status(422).json(errors.array())
    tripsService.getTrips()
    .then(trips =>
        res.status(200).json({
            exception: false,
            payload: trips
        })
    );
})

执行时似乎会产生“将循环结构转换为 JSON”错误

这是错误堆栈:

(node:9322) UnhandledPromiseRejectionWarning: TypeError: Converting circular structure to JSON at JSON.stringify () at o.getTrips.then.e (/home/sandra/development/lakefrontcargo-v2/dist/index.js:1:57753) at (node:9322) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1) (node:9322) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code. [nodemon] restarting due to changes...

最佳答案

由于 map 返回 Promise 数组,因此我建议您使用 Promise.all 来等待所有 Promise 完成。

const data  = Promise.all ( trips.map(trip => ({
    ...trip,
    milestones: async () => await Milestone.findAll({
        where: {
            trips_id: trip.id
        }
    }),
    vendor_charges: async () => await VendorCharge.findAll({
        where: {
            trips_id: trip.id
        }
    }),
    trip_notes: async () => await TripNote.findAll({
        where: {
            trips_id: trip.id
        }
    }),
    pieces: async () => await Pieces.findAll({
        where: {
            trips_id: trip.id
        }
    })
})) );


return await data;

关于javascript - express 和sequelize 将循环结构转换为JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54232872/

相关文章:

node.js - 在express中有选择地应用中间件

node.js - express 4 : How to upload file to memory (e. g。作为 UTF-8 字符串)而不是磁盘?

mysql - 如何在express.js中使用sequelize在mysql表中插入数据

java - 需要将Java中的数组传递给javascript

JavaScript 继承与 jQuery - 调用父函数

Javascript 从数组中获取对象序列

javascript - Tslint 升级错误

node.js - 将参数注入(inject) npm 脚本命令

javascript - 使用 Cowboy 作为 Express JS 的 HTTP 网络服务器

javascript - jquery使用正则表达式替换 anchor 中的字符串