云函数已经部署成功了,之前用都是可以的 现在用就出问题了
app.js中的env也是对的 ,和云环境对上了。 都没改过
Error: errCode: -404011 cloud function execution error | errMsg: cloud.callFunction:fail requestID e28cf76b-07e2-11eb-be59-525400c2bfee, cloud function service error code -504002, error message Error: errCode: -1 unknown error | errMsg: Cloud API isn't enabled, please call init first
请先调用 init 完成初始化后再调用其他云 API。init 方法可传入一个对象用于设置默认配置,详见文档。
at Object.args [as database] (:39586/var/user/node_modules/wx-server-sdk/index.js:119)
at Object.<anonymous> (:39586/var/user/index.js:3)
at Module._compile (:39586/appservice/internal/modules/cjs/loader.js:701)
at Object.Module._extensions..js (:39586/appservice/internal/modules/cjs/loader.js:712)
at Module.load (:39586/appservice/internal/modules/cjs/loader.js:600)
at tryModuleLoad (:39586/appservice/internal/modules/cjs/loader.js:539)
at Function.Module._load (:39586/appservice/internal/modules/cjs/loader.js:531)
at Module.require (:39586/appservice/internal/modules/cjs/loader.js:637)
at require (:39586/appservice/internal/modules/cjs/helpers.js:22)
at Object.module.exports.load (:39586/var/runtime/node10/UserFunction.js:27); at cloud.callFunction api;
at new u (WAService.js:1)
at d (WAService.js:1)
at f (WAService.js:1)
at Function.success (WAService.js:1)
at WAService.js:1
at C (WAService.js:1)
at i.<anonymous> (WAService.js:1)
at i.emit (WAService.js:1)
at Rs (WAService.js:1)
at WAService.js:1
云函数
// 云函数入口文件
const cloud = require('wx-server-sdk')
const db = cloud.database();//链接数据库
const _ = db.command
cloud.init({
// API 调用都保持和云函数当前所在环境一致
env: 'hstest-0t7e4'
})
// 云函数入口函数
exports.main = async (event, context) => {
const { OPENID } = cloud.getWXContext()
const userCollection = db.collection('parent')//选取数据库
const thisID = userCollection.where({ '_openid': OPENID })
return thisID.get().then(res => {
if (res.data.length === 0) {//如果不存在 - 新增 res.data[0].score
return userCollection.add({
data: event.data_list
})
} else {//如果表存在字段 - 更新
return thisID.update({
data: event.data_list
})
}
})
}
init写到上面就行了