promise mongoose loop through query - Stack Overflow
天蓬老师
天蓬老师 2017-06-29 10:09:23
0
2
1015
let result = []; //存放查询结果 model.WithdrawModel.find({status:'processing'}, (err, doc) => { if (err) { console.log(err); res.json({ code: -1, msg: '查询失败'}); return; } else { doc.map((item) => { model.UserModel.findOne({phone:item.phone},'name IDcard bank bankCard bank_area bank_name', (err, bankInfo) => { if (err) { console.log(err); } else { let obj = {}; Object.assign(obj, JSON.parse(JSON.stringify(item)), JSON.parse(JSON.stringify(bankInfo))); result.push(obj); console.log(result); } }) }); res.json({ code: 0, msg: '查询成功', result: result}); return; } });

Looping through the query, the above result directly returns a null value. How can I ensure that all the internal queries in doc.map are completed before taking out the value of result?

天蓬老师
天蓬老师

欢迎选择我的课程,让我们一起见证您的进步~~

reply all (2)
仅有的幸福

Ask and answer your own questions, and receive guidance from others.

promise.all is implemented as follows:

let result = []; //存放查询结果 let doc1 = []; //存放第一次查询的结果 model.WithdrawModel.find({status:'processing'}).exec().then((doc) => { doc1 = doc; const promises = doc.map(item => model.UserModel.findOne({phone:item.phone},'name IDcard bank bankCard bank_area bank_name')); return Promise.all(promises); }) .then((bankInfoList) => {//promise.all返回的结果是一一对应的 for(let i=0; i { resolve(result); }) }) .then((result) => { return new Promise(() => { res.json({ code: 0, msg: '查询成功', result: result}); return; }); }) .catch((e) => { console.log(e); res.json({ code: -1, msg: '查询失败'}); return; });

-------------------------------------------------Supplement---------- ---------------------------------------

The idea of counting is still achievable, just use the event module:

    代言

    Since Promise is an asynchronous call, usingreturnafter all queries will return before the data is actually obtained, so you need to add a counter count in the Promise, and add a loop under all Promises. All docs have been cycled to (count == doc.length) before they can be output and returned.

    const deasync = require('deasync'); // 引入deasync包 ... let result = []; //存放查询结果 model.WithdrawModel.find({status: 'processing'}, (err, doc) => { if(err) { console.log(err); res.json({code: -1, msg: '查询失败'}); return; } else { let count = 0, len = doc.length; doc.forEach((item, index) => { model.UserModel.findOne({phone: item.phone}, 'name IDcard bank bankCard bank_area bank_name', (err, bankInfo) => { if (err) { console.log(err); } else { let obj = {}; Object.assign(obj, JSON.parse(JSON.stringify(item)), JSON.parse(JSON.stringify(bankInfo))); result.push(obj); console.log(result); } count++; }); }); deasync.loopWhile(() => count < len); res.json({code: 0, msg: '查询成功', result: result}); return; } });
      Latest Downloads
      More>
      Web Effects
      Website Source Code
      Website Materials
      Front End Template
      About us Disclaimer Sitemap
      php.cn:Public welfare online PHP training,Help PHP learners grow quickly!