微信小程序错误 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: ''
  }
})
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叶绿体不忘呼吸

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值