Javascript TypeError:无法读取属性';设置状态';空值,直接从firebase rtdb获取数据
为什么我的状态不使用我直接从direbase获取的数据进行更新 我在控制台中获取数据的地方 我做错了什么, 错误就在这里 更改getData()方法定义,如下所示:Javascript TypeError:无法读取属性';设置状态';空值,直接从firebase rtdb获取数据,javascript,reactjs,firebase,firebase-realtime-database,Javascript,Reactjs,Firebase,Firebase Realtime Database,为什么我的状态不使用我直接从direbase获取的数据进行更新 我在控制台中获取数据的地方 我做错了什么, 错误就在这里 更改getData()方法定义,如下所示: getData = () => { ... As the above code } 或 将其绑定到构造函数中的getData方法 constructor() { super(props); this.state = { email: '', firstName: '', lastName: '' }
getData = () => {
... As the above code
}
或
将其绑定到构造函数中的getData方法
constructor() {
super(props);
this.state = { email: '', firstName: '', lastName: '' }
this.getData = this.getData.bind(this); // Binding this context to getData method
}
更改getData()方法定义,如下所示
getData = () => {
... As the above code
}
或
将其绑定到构造函数中的getData方法
constructor() {
super(props);
this.state = { email: '', firstName: '', lastName: '' }
this.getData = this.getData.bind(this); // Binding this context to getData method
}
这里的问题是您的
此
不在范围内,这里的此
指的是firebase回调范围。您可以控制台此
并进行检查
const db = firebase.database().ref('users/trainers/'+uid).on('value', function(snapshot){
// here this refers to the firebase callback scope
this.setState({
.........
.........
})
}))
你能做的就是
const that = this;
const db = firebase.database().ref('users/trainers/'+uid).on('value', function(snapshot){
// now `that` holds your class LogPro1 scope, now you can setState using that.setState()
that.setState({
.........
.........
})
}))
这里的问题是您的
此
不在范围内,这里的此
指的是firebase回调范围。您可以控制台此
并进行检查
const db = firebase.database().ref('users/trainers/'+uid).on('value', function(snapshot){
// here this refers to the firebase callback scope
this.setState({
.........
.........
})
}))
你能做的就是
const that = this;
const db = firebase.database().ref('users/trainers/'+uid).on('value', function(snapshot){
// now `that` holds your class LogPro1 scope, now you can setState using that.setState()
that.setState({
.........
.........
})
}))
更改此项:
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({
.........
.........
})
}))
使用箭头功能:
箭头函数对此没有自己的定义。使用封闭词法范围的此值;箭头函数遵循普通变量查找规则。因此,当搜索当前范围中不存在的this时,arrow函数最终从其封闭范围中找到this
更改此项:
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({
.........
.........
})
}))
使用箭头功能:
箭头函数对此没有自己的定义。使用封闭词法范围的此值;箭头函数遵循普通变量查找规则。因此,当搜索当前范围中不存在的this时,arrow函数最终从其封闭范围中找到this
在堆栈溢出时,请不要显示文本和代码的图片。您应该将文本复制到问题本身中,以便更易于阅读和搜索。在堆栈溢出时,请不要显示文本和代码的图片。你应该将文本复制到问题本身中,以便更容易阅读和搜索。我以前处理过bind,但这次它给了我“无法读取未定义的属性‘bind’”+你的第一个解决方案不起作用我以前处理过bind,但这次它给了我“无法读取未定义的属性‘bind’”+您的第一个解决方案也不起作用