繁体   English   中英

Mongoose Express.js 从多个查询中获取结果

[英]Mongoose Express.js get result from multiple queries

我正在尝试使用异步方法从 mongodb 的一系列查询中获取结果,但不幸的是,我生成的 object 在异步 function scope 之外未定义。

      let dataObject = {
         prop1: 1,
         prop2: 2
         ....
      };

      let result = {};

      _.each(dataObject, async function (val, key) {

        let query = [
          {
            $match: {
              x:1
              ....
            }
          }
        ];

        let = MyModel.aggregate(query);
        let data = await q.exec();

          if (data.length > 0) {
            result[key] = data[0].myProp;
            console.log(result); // I can see result here
          }
       });

       console.log('====>>', result); // Here the result is undefined

我缺少什么?

对于这个问题,你可以创建一个 promise function 例如getData , dataObject 作为输入参数和 function 返回结果如下:

let async = require('async');
let q = require('q');

const getData = async (dataObject)=>{
  let defer =q.defer();
  let result = []
  input = Object.keys(dataObject)
  async.eachSeries(input , async(key)=>{
    try {
      let query = [
        {
          $match: {
            x:1
            ....
          }
        }
      ];
      let data = await MyModel.aggregate(query).lean();
      if (data.length > 0) {
        result[key] = data[0].myProp;
        console.log(result);
      }
    } catch (error) {
      console.log(error)
    }
  },()=>{
    console.log("finish process")
    defer.resolve(result)// return final data
  })
  return defer.promise
}

在 main function 中调用此函数 ( getData ),如下所示:

const mainFunctio = async()=>{
  dataObject
  console.log(start)
  result  = await getData(dataObject)
  console.log(result)
}

I am trying to get results from a series of queries on mongodb using async method but unfortunately the object that I generate is undefined outside the async function scope.

      let dataObject = {
         prop1: 1,
         prop2: 2
         ....
      };

      let result = {};

      _.each(dataObject, async function (val, key) {

        let query = [
          {
            $match: {
              x:1
              ....
            }
          }
        ];

        let = MyModel.aggregate(query);
        let data = await q.exec();

          if (data.length > 0) {
            result[key] = data[0].myProp;
            console.log(result); // I can see result here
          }
       });

       console.log('====>>', result); // Here the result is undefined

我错过了什么?

作为参考,我发布了一个我找到的看起来非常简单的解决方案。 我没有使用 _.each 运算符,而是尝试使用一个简单的 for 循环并删除了 exec() (正如 Mohammad 所建议的)并且它按预期工作:

  let dataObject = {
     prop1: 1,
     prop2: 2
     ....
  };

  let result = {};
  let allKeys = Object.keys(dataObject)
   for (key of allKeys) { {

    let query = [
      {
        $match: {
          x:1
          ....
        }
      }
    ];

    let data = MyModel.aggregate(query);

      if (data.length > 0) {
        result[key] = data[0].myProp;
      }
   });

   console.log('====>>', result); // works fine!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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