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

发布于 2022-09-12 03:53:46 字数 658 浏览 18 评论 0

我在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);
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

嘿嘿嘿 2022-09-19 03:53:46

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

把回忆走一遍 2022-09-19 03:53:46

仔细看文档嗯

这样写

let resCustomer= await db.collection('user').where({
  _openid:getApp().globalData.openid
}).get()

that.setData({
 num1:resCustomer.data
})
console.log("resCustomer:"+resCustomer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文