Thank you. That's it.
uniapp wechat applet call wechat cloud development server returned data how to assign value to the data variable?
data(){
return{
rentlists="13123"
}
}
created(){
const db = wx.cloud.database()
db.collection('myRent').where({
type: 'info'
}).get({
success: function(res) {
let that=this
console.log(that.rentlists); // undefined? 预期应该是rentlists="13123"
// 输出 [{ "title": "The Catcher in the Rye", ... }]
that.rentlists=res.data; //能接收到数据、
console.log(that.rentlists) //此时res.data的确赋值给rentlists 但这个rentlists似乎不是上方data中的rentlists?
}
})
},
0 Answer
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.
The first console is OK, the second console is still the initial value why? res.data can console but cannot be assigned
这家伙很懒,什么都没留下...