云函数本地调试报错: 函数必须导出名为 'main' 的方法?
发布于 5 年前 作者 min74 10800 次浏览 来自 问答

在修改完云函数保存更新后,本地调试报错:

云函数代码如下:

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database('Insurance')
const MAX_LIMIT = 10
exports.main = async (event, context) => {
  // 先取出集合记录总数
  const countResult = await db.collection('Insurance').count()
  const total = countResult.total
  // 计算需分几次取
  const batchTimes = Math.ceil(total / 10)
  // 承载所有读操作的 promise 的数组
  const tasks = []
  for (let i = 0; i < batchTimes; i++) {
    const promise = db.collection('Insurance').skip(i * MAX_LIMIT).limit(MAX_LIMIT).get()
    tasks.push(promise)
  }
  // 等待所有
  return (await Promise.all(tasks)).reduce((acc, cur) => {
    return {
      data: acc.data.concat(cur.data),
      errMsg: acc.errMsg,
    }
  })
}
回到顶部