为什么用this.setData()创建和赋值数组失败?
发布于 4 年前 作者 leihu 2661 次浏览 来自 官方Issues

请问有哪位朋友知道这个问题该如何解决?

跑下面的代码时,可以获取到想要的database里的数据

onLoad: function (options{
    var that = this;
    db.collection("zixun_list").where({
      num:options.id
    }).get()
    // .then(res=>{
    //   that.setData({
    //     dataobj:res.data
    //   })
    //   console.log(dataobj)
    // })
    .then(res=>{
      console.log(res.data)
    })
  },

但是,如果是这样就会报错,

onLoad: function (options{
    var that = this;
    console.log(options.id)
    db.collection("zixun_list").where({
      num:options.id
    }).get()
    .then(res=>{
      that.setData({
        dataobj:res.data
      }),
      console.log(dataobj)
    })
    // .then(res=>{
    //   console.log(res.data)
    // })
  },

为什么会说dataobj is not defined呢?

我最终是想要获取数据库里的数据然后赋值给dataobj,并渲染到前端去。

2 回复
console.log(that.data.dataobj)

dataobj你之前定义了嘛

回到顶部