gpt4 book ai didi

javascript - 直接从 firebase rtdb 获取数据时出现 TypeError : Cannot read property 'setState' of null,

转载 作者:行者123 更新时间:2023-12-01 00:12:57 26 4
gpt4 key购买 nike

为什么我的状态没有使用我直接从 direbase 获取的数据进行更新 enter image description here

我在控制台中获取数据的位置

enter image description here

我在这里做错了什么,错误在这里 enter image description here

最佳答案

更改此:

const db = firebase.database().ref('users/trainers/'+uid).on('value', function(snapshot){
// here this refers to the firebase callback scope
this.setState({
.........
.........
})
}))

进入此:

const db = firebase.database().ref('users/trainers/'+uid).on('value', (snapshot) =>{
// here this refers to the firebase callback scope
this.setState({
.........
.........
})
}))

使用箭头函数:

An arrow function does not have its own this. The this value of the enclosing lexical scope is used; arrow functions follow the normal variable lookup rules. So while searching for this which is not present in current scope, an arrow function ends up finding the this from its enclosing scope.

关于javascript - 直接从 firebase rtdb 获取数据时出现 TypeError : Cannot read property 'setState' of null,,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59943344/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com