微信小程序错误 Cloud API isn‘t enabled, please call wx.cloud.init first 解决

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', //填上你的云开发环境id
  traceUser: true,
})
const db = wx.cloud.database()
//请关注上面的,下面与该问题无关

App({
  initUiGlobal() {
    return new Promise(resolve => {
      wx.getSystemInfo({
        success: e => {
          this.globalData.StatusBar = e.statusBarHeight
          this.globalData.screenHeight = e.screenHeight
          const capsule = wx.getMenuButtonBoundingClientRect()
          if (capsule) {
            this.globalData.Custom = capsule
            this.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.openId
        db.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: ''
  }
})

你可能感兴趣的:(杂7杂8,微信小程序,小程序,前端,云开发)