0
Follow
0
View

The data obtained from the uniapp wechat cloud development database cannot be assigned to the data variable

dyfdyfdyf 注册会员
2023-02-28 06:52

Thank you. That's it.

diss33 注册会员
2023-02-28 06:52

This answer quotes ChatGPT

In your code, the data variable in the Vue instance can be accessed through the that variable, but that variable is defined inside the success function, and the scope of this keyword is different inside the success function. this causes that.rentlists and this.rentlists to refer to different variables.

To access the data variable of the Vue instance in the success function, you can first bind this of the Vue instance to another variable, and then access the data variable through this variable in the success function. You can modify the code as follows:


data() {
  return {
    rentlists: "13123"
  };
},
created() {
  const db = wx.cloud.database();
  let vm = this;
  db.collection('myRent').where({
    type: 'info'
  }).get({
    success: function (res) {
      console.log(vm.rentlists);  // vm 变量引用的是 Vue 实例,可以通过它来访问 data 变量
      vm.rentlists = res.data;
      console.log(vm.rentlists);
    }
  })
}

In the above code, I use the vm variable to refer to the Vue instance, which is used to access the data variable inside the success function.

fengjing1215 注册会员
2023-02-28 06:52

The first console is OK, the second console is still the initial value why? res.data can console but cannot be assigned