调用云函数,返回后赋值不同步?
发布于 5 年前 作者 lhe 1847 次浏览 来自 问答
data: {
  rec: []
},

onLoad() {
    wx.showLoading({
      title: '连接中...',
    })
    wx.cloud.callFunction({
        name: 'Share',
        config: {
          env: 'llu-j197cfaukdlsikd'
        },
        data: {
          type: 'selectRecord',
          query: "db.collection('books').get()"
        }
      }).then(res => {
        this.setData({
          rec: res.result.data,            //读取数据库返回结果复制给 rec
          wr: this.reads("Jone"),          //自定义函数reads中,获取不到rec
        })
       console.log(this.data.rec)          //这里可以显示rec获取的值,说明能获取到
        wx.hideLoading()
      })
      .catch((e) => {
        wx.showToast({
          title: e,
          duration: 3000,
          mask: true
        })
      })
  },

reads: function (qh) {
console.log(this.data.rec)    //结果显示为[],空的数组
}

怎么能在变量 rec 获取数据后,才调用自定义函数 reads() ? 谢谢!

1 回复

你分开写不就行了

回到顶部