小程序云函数collection参数能不能使用变量

发布于 7 年前作者 ping737655 次浏览最后编辑 7 年前来自 ask
  • 需求的场景描述(希望解决的问题)

exports.main = async(event, context) => {

console.log(event.id, event.name)

const dbName = event.name

// 先取出集合记录总数

const countResult = await db.collection(dbName).where({

areaid: _.eq(event.id)

}).count()

}

上面是部分代码,会报错误

Reduce of empty array with no initial value

dbName用具体的数据库名就能正常运行

请问是否这个命令不能使用变量代替数据库名?

1 回复
li91
li911 楼6 年前

Reduce of empty array with no initial value

你发的代码没问题,这个错误是reduce的初始值未设置,你代码里用了reduce吧