云函数调用云数据库lookup,一直返回undefined

2020-06-09  本文已影响0人  大胡子的机器人

一直返回报错的写法如下:

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  await db.collection('br_collect').aggregate()
    .lookup({
      from: 'br_article',
      localField: 'aid',
      foreignField: '_id',
      as: 'list',
    }).match({
      openid: openid
    })
    .end()
    .then(res => {
      console.log('success', res)
       return {
         code:200,
         message:'success',
         data:res
       }
    })
    .catch(err => {
      return {
        code: 0,
        message: err
      }
    })
}

修改后能正确返回的写法:

// 云函数入口文件
const cloud = require('wx-server-sdk')

cloud.init()
const db = cloud.database()
// 云函数入口函数
exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  const openid = wxContext.OPENID
  var listPromise = await db.collection('br_collect').aggregate()
    .lookup({
      from: 'br_article',
      localField: 'aid',
      foreignField: '_id',
      as: 'list',
    }).match({
      openid: openid
    })
    .end()
    console.log('listPromise=',listPromise)
    return {
      code:200,
      message:'success',
      data:listPromise
    }
}

上一篇下一篇

猜你喜欢

热点阅读