简体   繁体   中英

Iterate sequelize query result set (Hapijs)

I have a code that returns a result. Normaly when I receive that result, I send it to the client and along the way it is converted to pure JSON object.

But now I need to do some operations on that result set, and then do another lookup in the database.

What I dont understand is the structure of the result set. How Do I properly iterate on it. I could extract the values manualy using a for loop, but I have a feeling that is not the way to do it.

This is the code that returns results:

 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);
        });

Image shows the result in debug mode. It has 4 objects in array: 在此输入图像描述

When you use model.findAll , the resultset that is returned is an array of 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 on each item, passing an options object with the value plain: true .

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

You want to avoid forEach operations because NodeJS runs on a single thread. This is wonderful because it forces us to code differently. So imagine this, when the forEach runs its hogs the CPU because it's a greedy synchronous operation. We need to share resources and always think about running in parallel.

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."

This code, in essence, waits for the previous record to be retrieved before moving on to the next. So the faster the CPU, the faster the output.

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; 

the returned JSON Object from sequelize

[
    {
        "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"
    } ]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM