Cloud API isn't enabled, please call wx.cloud.init first
这个错误的意思就是云环境还没有初始化就调用其它的云api了,因此需要先初始化,也就是让我们先 wx.cloud.init()
进行初始化,最简单的办法就是直接在最前面初始化,代码如下:(只需要关注第3~9行即可)
import User from './model/user'
import $ from './utils/tool'//请关注下面的,上面两行与该问题无关
wx.cloud.init({env: 'zaiyi-3ggp5zmqe2dd21e7', //填上你的云开发环境idtraceUser: true,
})
const db = wx.cloud.database()
//请关注上面的,下面与该问题无关App({initUiGlobal() {return new Promise(resolve => {wx.getSystemInfo({success: e => {this.globalData.StatusBar = e.statusBarHeightthis.globalData.screenHeight = e.screenHeightconst capsule = wx.getMenuButtonBoundingClientRect()if (capsule) {this.globalData.Custom = capsulethis.globalData.CustomBar = capsule.bottom + capsule.top - e.statusBarHeight} else {this.globalData.CustomBar = e.statusBarHeight + 50}},complete: resolve})})},async login() {$.loading()const user = new User()wx.cloud.callFunction({name: 'getOpenid', // 对应云函数名complete: res => {console.log(res.result.event.userInfo.openId)this.globalData.openid = res.result.event.userInfo.openIddb.collection('user').where({_openid:res.result.event.userInfo.openId}).get().then(res => {console.log(res.data.length)if(res.data.length == 0){user.register()}})}})$.hideLoading()},async onLaunch() {await this.initUiGlobal()this.login()},globalData: {StatusBar: null,Custom: null,CustomBar: null,screenHeight: null,env: 'zaiyi-3ggp5zmqe2dd21e7',openid: ''}
})