迭代sequelize查询结果集(Hapijs)

Iterate sequelize query result set (Hapijs)

我有一个代码 returns 结果。通常,当我收到该结果时,我会将其发送给客户端,并在此过程中将其转换为纯 JSON 对象。

但现在我需要对该结果集进行一些操作,然后再在数据库中进行一次查找。

我不明白的是结果集的结构。我如何正确地迭代它。我可以使用 for 循环手动提取值,但我觉得这不是这样做的方法。

这是 returns 结果的代码:

 models.Results.findAll({
            where: {ProjectId: projectId}
        })
        .then(function (resultset) {              
            //How do I properly iterate over the resultset
            for(p in resultset){

                var a = p;
                var something;

            }


            reply(resultset).code(200);
        }, function (rejectedPromiseError) {
            reply(rejectedPromiseError).code(401);
        });

图像显示了调试模式下的结果。它在数组中有 4 个对象:

当您使用 model.findAll 时,返回的 resultset 是每个项目的 model Instance objects. If you want to get at just the interesting stuff (the actual values in your table), you can iterate over resultset and call the get function 数组,并传递值为 plain: true 的选项对象。

resultset.forEach((resultSetItem) => {
    console.log(resultSetItem.get({
        plain: true
    }));
});

您想避免 forEach 操作,因为 NodeJS 在单线程上运行。这很棒,因为它迫使我们以不同的方式编码。所以想象一下,当 forEach 运行它的猪 CPU 时,因为它是一个贪婪的同步操作。我们需要共享资源,总是并行思考运行。

http://bluebirdjs.com/docs/api/promise.each.html

"Iterate over an array, or a promise of an array, which contains promises (or a mix of promises and values) with the given iterator function with the signature (value, index, length) where value is the resolved value of a respective promise in the input array. Iteration happens serially. If the iterator function returns a promise or a thenable, then the result of the promise is awaited before continuing with next iteration. If any promise in the input array is rejected, then the returned promise is rejected as well."

本质上,这段代码等待检索到上一条记录,然后再继续下一条记录。所以CPU越快,输出越快

notification.sendAll = (message, cb) => {
    db.models.users.findAll().then(users => {
        db.Promise.each(users, user => {
            console.log('user id: ' + user.id)
            notification.sendMessage(message, ret => {
            })
            return
        })
    })
}

await Request.findAll({
         where: {
             S_Id: 13, 
             Customer_Id:req.body.Customer_Id,
         }
     }).then(function (results) {
         res.send(results[0]["CardNumber"])
         quitFunction.status =true;

从 sequelize

返回的 JSON 对象
[
    {
        "id": 1,
        "S_Id": 13,
        "Customer_Id": 4,
        "CardNumber": 345345,
        "createdAt": "2019-04-02T19:16:35.000Z",
        "updatedAt": "2019-04-02T19:24:41.000Z"
    },
    {
        "id": 2,
        "S_Id": 13,
        "Customer_Id": 4,
        "CardNumber": 345345,
        "createdAt": "2019-04-02T19:24:48.000Z",
        "updatedAt": "2019-04-02T19:35:26.000Z"
    },
    {
        "id": 3,
        "ServicAction_Id": 13,
        "Customer_Id": 4,
        "CardNumber": 345345,
        "createdAt": "2019-04-02T19:39:40.000Z",
        "updatedAt": "2019-04-04T20:03:52.000Z"
    },
    {
        "id": 4,
        "ServicAction_Id": 13,
        "Customer_Id": 4,
        "CardNumber": 345345,
        "createdAt": "2019-04-04T20:08:11.000Z",
        "updatedAt": "2019-04-04T20:08:11.000Z"
    },
    {
        "id": 5,
        "ServicAction_Id": 13,
        "Customer_Id": 4,
        "CardNumber": 345345,
        "createdAt": "2019-04-05T18:53:34.000Z",
        "updatedAt": "2019-04-05T18:53:34.000Z"
    },
    {
        "id": 6,
        "S_Id": 13,
        "Customer_Id": 4,
        "CardNumber": 345345,
        "createdAt": "2019-04-05T18:54:32.000Z",
        "updatedAt": "2019-04-05T18:54:32.000Z"
    },
    {
        "id": 7,
        "S_Id": 13,
        "Customer_Id": 4,
        "CardNumber": 345345,
        "createdAt": "2019-04-05T18:54:57.000Z",
        "updatedAt": "2019-04-05T18:54:57.000Z"
    } ]