Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
728 views
in Technique[技术] by (71.8m points)

微信小程序用自带云开发实现,获取得到数据库数据无法赋值到全局变量怎么办?

我在data中定义一个num1数组,在getBarOption函数中获取数据库数据,想把这个数据赋值给num1,但是无法完成赋值。网上说是异步问题,我尝试了增加async和await,然后勾选“增强编译”,但是还是不行,报错说resCustomer.data中的data未定义。我用console.log(resCustomer)打印结果也是未定义,但是res是能获取到数据的。这是为什么?请问怎么解决这个问题呀?怎么才能赋值给num1呀?求大佬们指点一二。

data: {
    num1:[],
},
async function  getBarOption() {
  const db = wx.cloud.database()
  let that=this;

  let resCustomer= await db.collection('user').where({
      _openid:getApp().globalData.openid
   }).get({
       success: res => {
           console.log('查到的值为', res)
        }
      })

  that.setData({
     num1:resCustomer.data
  })
  console.log("resCustomer:"+resCustomer);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

想了好久还是没思路,求大佬们给点意见。感谢


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...