【问题标题】:Node js Nested Promise.all errorNode js嵌套Promise.all错误
【发布时间】:2018-07-12 14:04:33
【问题描述】:
exports.AllDataCounts= function(req, res) {
  var UserId = req.params.User_Id;
  Promise.all([
           Model.SchemaOne.count({ User_Id: UserId }).exec(),
           Model.SchemaTwo.find({ User_Id: UserId }).exec()
  ]).then(response_One => {
     console.log('response_One Success');
     var _ids = response_One[1].map(x => x._id );

     const GetMoreInfo = _id => {
             Promise.all([
                  Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
                  Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
             ]).then( response_three => {
                 console.log('response_three Success');
                 return response_three ;
              }).catch(error_3 => {
                 console.log('error');
                 return Cube_Id;
               });  
     };
     Promise.all(
           _ids.map(_id=> GetMoreInfo(_id))
     ).then(response_two => {
        console.log('response_two Success'); 
        res.send({ Status:true, Response: { Output1: response_One, Output3: response_two });
     }).catch( error_two = > {
        res.send({ Status: false, Error: error_two, Message: 'Some Error Occurred' });
     });
  }).catch( error_one = > {
     res.send({ Status: false, Error: error_1, Message: 'Some Error Occurred' });
  });
};

我希望控制台输出是

response_一个成功

response_three 成功

response_Two 成功

但我得到的结果是

response_一个成功

response_Two 成功

response_三成功

如果我将删除 Promise.all 中的 GetMoreInfo 函数,它会正常工作

【问题讨论】:

    标签: javascript node.js promise es6-promise


    【解决方案1】:

    你有:

    const GetMoreInfo= _id => {
      Promise.all([
        Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
        Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
      ]).then( 
    

    getMoreInfo 没有返回它的Promise.all,所以当你调用它时它没有被正确地包含在更大的承诺链中

    Promise.all(
      _ids.map(_id=> GetMoreInfo(_id))
    ).then( ...
    

    (相反,目前那里的.then 将立即解决,因为Promise.all 正在undefineds 的数组上被调用,这不是您想要的)

    改为:

    const GetMoreInfo= _id => {
      return Promise.all([
        // ...
    

    【讨论】:

      猜你喜欢
      • 2016-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-06-24
      • 2016-07-25
      相关资源
      最近更新 更多