首页 文章

迭代sequelize查询结果集(Hapijs)

提问于
浏览
2

我有一个返回结果的代码 . Normaly当我收到这个结果时,我将它发送给客户端,并将其转换为纯JSON对象 .

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

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

这是返回结果的代码:

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个对象:
enter image description here

3 回答

  • 1

    当您使用 model.findAll 时,返回的 resultsetmodel Instance objects的数组 . 如果你想获得有趣的东西(表中的实际值),你可以迭代 resultset 并在每个项目上调用get function,传递一个值为 plain: true 的options对象 .

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

    您希望避免forEach操作,因为NodeJS在单个线程上运行 . 这很棒,因为它迫使我们采用不同的代码 . 所以想象一下,当forEach运行它的CPU时,因为它是一个贪婪的同步操作 . 我们需要共享资源并始终考虑并行运行 .

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

    “迭代数组,或数组的承诺,其中包含promises(或promises和values的混合)与给定迭代器函数的签名(值,索引,长度),其中value是相应promise的解析值在输入数组中 . 迭代按顺序发生 . 如果迭代器函数返回一个promise或一个thenable, then the result of the promise is awaited before continuing with next iteration . 如果输入数组中的任何promise被拒绝,那么返回的promise也会被拒绝 . “

    实质上,此代码在继续下一个之前等待检索先前的记录 . 因此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
            })
        })
    }
    
  • 8
    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"
        } ]
    

相关问题