收藏
回答

循环调用数据库上百次出现报错的情况,没找到原因?

环境:云开发

配额方案:基础版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次),想知道通过怎么优化代码实现以上功能

请知道的大神指教,谢谢。

回答关注问题邀请回答
收藏

1 个回答

  • xplee
    xplee
    2020-11-06
    1. 使用in操作符,
    2. 数据量太大的话,建议采用分批处理的办法(每个分批中使用Promise)


    let personIds = list.map(item => {

    return item.personId

    })

    const _=db.command

    db.collection('person_list').where({

    _id: _.in( personIds );

    }).get()


    2020-11-06
    有用
    回复
登录 后发表内容
问题标签