收藏
回答

云函数操作数据库报错什么原因? 500 Internal error

定时触发的云函数, 总数据700多条,每150条做一次1s的延时,结束会报几条下面的错误。

代码:

  const uploadTasks = []
  dbUsers.forEach(function (user) {
    let key = (user._id) + "-" + dateStr
    const promise = tCountLog.add({
      data: {
        "xxx":"xxx"
      },
    })
    uploadTasks.push(promise)
  })

  for (i = 0; i < Math.ceil(uploadTasks.length / 150.0); i++) { // 每150条一个循环
    const theTasks = uploadTasks.slice(150 * i, Math.min(150 * (i + 1), uploadTasks.length));
    log.info({
      'title': 'theTasks--theTasks.length=' + theTasks.length
    })
    await Promise.all(theTasks);
    await sleep(1000);
  }
  return {
    data: {},
    code: 0,
  }

function sleep(time) {
  return new Promise((resolve) => setTimeout(resolve, time));
}


报错:

node.js:1 [error] Unhandled Rejection at: Promise {<rejected>: Error: errCode: -1 unknown error | errMsg: collection.add:fail Error: 
            500 Internal Ser…}__proto__: Promise[[PromiseStatus]]: "rejected"[[PromiseValue]]: Error: errCode: -1 unknown error | errMsg: collection.add:fail Error: 
            500 Internal Server Error
          ; at collection.add api; 
    at returnAsCloudSDKError (/Users/kekejin/Documents/KWAI/KWAI-fans/cloudfunctions/updateCountLog/node_modules/wx-server-sdk/index.js:6086:16)
    at Object.returnAsFinalCloudSDKError (/Users/kekejin/Documents/KWAI/KWAI-fans/cloudfunctions/updateCountLog/node_modules/wx-server-sdk/index.js:6100:15)
    at /Users/kekejin/Documents/KWAI/KWAI-fans/cloudfunctions/updateCountLog/node_modules/wx-server-sdk/index.js:1198:32
    at processTicksAndRejections (internal/process/task_queues.js:85:5) reason: Error: errCode: -1 unknown error | errMsg: collection.add:fail Error: 
            500 Internal Server Error
          ; at collection.add api; 
    at returnAsCloudSDKError (/Users/kekejin/Documents/KWAI/KWAI-fans/cloudfunctions/updateCountLog/node_modules/wx-server-sdk/index.js:6086:16)
    at Object.returnAsFinalCloudSDKError (/Users/kekejin/Documents/KWAI/KWAI-fans/cloudfunctions/updateCountLog/node_modules/wx-server-sdk/index.js:6100:15)
    at /Users/kekejin/Documents/KWAI/KWAI-fans/cloudfunctions/updateCountLog/node_modules/wx-server-sdk/index.js:1198:32
    at processTicksAndRejections (internal/process/task_queues.js:85:5)
_ @ node.js:1
(anonymous) @ node.js:1
emit @ events.js:203
processPromiseRejections @ internal/process/promises.js:201
processTicksAndRejections @ internal/process/task_queues.js:86
internal/process/warning.js:27 (node:26682) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.

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

1 个回答

  • o0o有脾气的酸奶
    o0o有脾气的酸奶
    06-28
    Unhandled Rejection
    

    没有处理Rejection

    await Promise.all(theTasks).catch(e=>{
        log.error({
          'error': e
        })
    })
    

    若认为该回答有用,给回答者点个[ 有用 ],让答案帮助更多的人

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