Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/445.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 节点js嵌套承诺。所有错误_Javascript_Node.js_Promise_Es6 Promise - Fatal编程技术网

Javascript 节点js嵌套承诺。所有错误

Javascript 节点js嵌套承诺。所有错误,javascript,node.js,promise,es6-promise,Javascript,Node.js,Promise,Es6 Promise,我希望控制台输出是 回答:你成功了吗 回应(三)成功 回应(二)成功 但我得到的结果是 回答:你成功了吗 回应(二)成功 回应(三)成功 如果我将在Promise.all中删除GetMoreInfo函数,它将正常工作您有: exports.AllDataCounts= function(req, res) { var UserId = req.params.User_Id; Promise.all([ Model.SchemaOne.count({ User_Id:

我希望控制台输出是

回答:你成功了吗

回应(三)成功

回应(二)成功

但我得到的结果是

回答:你成功了吗

回应(二)成功

回应(三)成功

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

您有:

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' });
  });
};
getMoreInfo
没有返回它的
承诺。all
,因此当您调用时,它没有正确地包含在更大的承诺链中

const GetMoreInfo= _id => {
  Promise.all([
    Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
    Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
  ]).then( 
(相反,当前,
.then
将立即解析,因为
承诺。所有的
都是在
未定义的
数组上调用的,这不是您想要的)

改为:

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

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' });
  });
};
getMoreInfo
没有返回它的
承诺。all
,因此当您调用时,它没有正确地包含在更大的承诺链中

const GetMoreInfo= _id => {
  Promise.all([
    Model.Cube_SchemaThree.count({ Cube_Id: _id }).exec(),
    Model.Cube_SchemaFour.count({ Cube_Id: _id }).exec(),
  ]).then( 
(相反,当前,
.then
将立即解析,因为
承诺。所有的
都是在
未定义的
数组上调用的,这不是您想要的)

改为:

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