云函数更新数据库调用成功返回null说找不到数据库表

发布于 8 年前作者 xiajing1045 次浏览最后编辑 8 年前来自 issues
3 回复
li77
li771 楼6 年前

云函数处理数据库并根据处理结果返回前端:

exports.main = async (event, context) => {
  const wxContext = cloud.getWXContext()
  try {
    let res =  await db.collection('tb_user').add({
      data: {
        openid: wxContext.OPENID,
        appid: wxContext.APPID,
        unionid: wxContext.UNIONID
      }
    })
    var code = 0
    var msg = 'success'
    if (res.errMsg != 'collection.add:ok') {
      code = 1
      msg = 'error'
    }
    return {
      code: code,
      msg: msg,
      data: {
        openid: wxContext.OPENID,
        appid: wxContext.APPID,
        unionid: wxContext.UNIONID,
        env: wxContext.ENV
      }
    }
  } catch (e) {
    console.error('err:',e)
    return {
      code: e.errCode,
      msg: 'error'
    }
  }
}
blin
blin2 楼6 年前

我找到原因了,谢谢大家。原来是我在云函数中没有指定环境名称,只有在小程序端指定了环境名称和初始化云数据库。

nading
nading3 楼6 年前

please check your request first?