环境:云开发
配额方案:基础版1
说明:以下list中会有上百条或者几百条数据
const promise = Promise.all(list.map(item => {
return new Promise(resolve => {
db.collection('person_list').where({
_id: item.personId,
state: 0
}).get().then(res=>{
resolve(res.data[0])
})
})
}))
Promise.all([promise]).then(resp=>{
console.log("promise", resp[0]) //此处数据量少的情况下能输出数据
}).catch(function(e){
console.log(e);
});
期望:能正常执行以上代码,并加载出数据
实际:执行完以上代码会出现如下两个报错:
报错1:
VM142:1 Uncaught (in promise) thirdScriptError
getaddrinfo ENOTFOUND servicewechat.com
Error: getaddrinfo ENOTFOUND servicewechat.com
at Function.fail (http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:284149)
at Object.fail (http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:126419)
at v (http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:529613)
at http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:530772
at n.<anonymous> (http://127.0.0.1:29570/appservice/__dev__/asdebug.js:1:24647)
at http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:126419
at http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:109342
报错2:
Uncaught (in promise) thirdScriptError
read ETIMEDOUT
Error: read ETIMEDOUT
at Function.fail (http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:284149)
at Object.fail (http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:126419)
at v (http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:529613)
at http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:530772
at n.<anonymous> (http://127.0.0.1:29570/appservice/__dev__/asdebug.js:1:24647)
at http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:126419
at http://127.0.0.1:29570/appservice/__dev__/WAService.js:2:109342
现已知的原因可能是超过数据库最大并发的连接次数(最大并发的连接次数为20次),想知道通过怎么优化代码实现以上功能
请知道的大神指教,谢谢。
let personIds = list.map(item => {
return item.personId
})
const _=db.command
db.collection('person_list').where({
_id: _.in( personIds );
}).get()