errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist.

微信小程序云函数开发报错:

Uncaught (in promise) thirdScriptError
errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.
Error: errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.
    at new u (http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:232879)
    at Function.success (http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:283581)
    at Object.success (http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:126419)
    at y (http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:528980)
    at v (http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:529218)
    at http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:530772
    at n.<anonymous> (http://127.0.0.1:50148/appservice/__dev__/asdebug.js:1:10293)
    at http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:126419
    at http://127.0.0.1:50148/appservice/__dev__/WAService.js:2:109342
console.error @ VM1199:1
errorReport @ VM1206 WAService.js:2
thirdErrorReport @ VM1206 WAService.js:2
(anonymous) @ VM1206 WAService.js:2
c @ VM1206 WAService.js:2
i @ VM1206 WAService.js:2
(anonymous) @ VM1206 WAService.js:2
value @ VM1206 WAService.js:2
t @ VM1206 WAService.js:2
(anonymous) @ VM1206 WAService.js:2

errCode: -502005 database collection not exists | errMsg: [ResourceNotFound] Db or Table not exist. Please check your request, but if the problem cannot be solved, contact us.

 

错误原因1:数据库没有对应的集合

那么问题来了,错误提示中并没有提示具体那一个集合没有,这时候我们可以去代码中找有哪些集合,微信开发者工具中全局搜“collection” 

然后打开开发工具的云开发,在云数据库中全部添加对应的集合

错误原因2:云函数与数据库没在一个环境

你有可能有两个云开发环境

if (!wx.cloud) {
      console.error('请使用 2.2.3 或以上的基础库以使用云能力')
    } else {
      wx.cloud.init({
        // 此处请填入环境 ID, 环境 ID 可打开云控制台查看
        env: '321',
        traceUser: true
      })
    }
}

但是在云函数里用的却是默认的初始化。

const cloud = require('wx-server-sdk')
cloud.init()
const db = cloud.database()
exports.main = async(event, context) => {
  try {
    return await db.collection('busers').add({
      data: {
        year: event.year,
        month: event.month
      }
    })
  }catch(e){
    console.error(e)
  }
}
相关推荐
实付 9.90元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值