// 云函数入口文件
const cloud = require('wx-server-sdk')
cloud.init()
const db=cloud.database()
const _ =db.command
const $ =_.aggregate
// 云函数入口函数
exports.main = async (event, context) => {
return await db.collection('class').aggregate()
.lookup({
from: 'student',
pipeline:$.pipeline()
.group({
_id: null,
score: $.avg('$score') //计算平均成绩
})
.done(),//表示结束定义
as: 'stu',
})
.match({
teacher:"徐老师"
})
.replaceRoot({
newRoot: $.mergeObjects([ $.arrayElemAt(['$stu', 0]), '$$ROOT' ])
}).project({
_id:0,
teacher:1,
score:1,
}).then(res=>{
console.log('这是云函数里面的结果',res)
})
.end()
}
失败的是这样的提示
调用状态
失败, ret code 1
返回结果
{"errorCode":1,"errorMessage":"user code exception caught","stackTrace":"TypeError: db.collection(...).aggregate(...).lookup(...).match(...).replaceRoot(...).project(...).then is not a function\n at Runtime.exports.main [as handler] (/var/user/index.js:33:6)\n at Runtime.handleOnce (/var/runtime/node12/Runtime.engine.js:219:23)\n at Timeout._onTimeout (/var/runtime/node12/Runtime.engine.js:56:12)\n at listOnTimeout (internal/timers.js:549:17)\n at processTimers (internal/timers.js:492:7)","statusCode":430}
真尴尬 end()结尾的
同样的云函数,写在两个云函数中,一个成功:
调用状态
成功
返回结果
{"list":[{"score":90,"teacher":"徐老师"}],"errMsg":"collection.aggregate:ok"}
一个不成功,怎么回事啊,不能同时调用,还是咱样的呢?