Javascript promise.then()中的setState(以及其他所有函数)未定义
我正在使用从Firebase获取的数据创建React应用程序 当我使用下面的函数删除记录时,我会尝试重新加载页面,以获得不包含已删除记录的更新视图Javascript promise.then()中的setState(以及其他所有函数)未定义,javascript,firebase,firebase-realtime-database,ecmascript-6,promise,Javascript,Firebase,Firebase Realtime Database,Ecmascript 6,Promise,我正在使用从Firebase获取的数据创建React应用程序 当我使用下面的函数删除记录时,我会尝试重新加载页面,以获得不包含已删除记录的更新视图 deleteWorkout(id, e) { e.preventDefault(); fire.database().ref("athlete") .child(id).remove() // <-- This works // This does not work! //Error: '_this4.getAthl
deleteWorkout(id, e) {
e.preventDefault();
fire.database().ref("athlete")
.child(id).remove() // <-- This works
// This does not work!
//Error: '_this4.getAthleteData' is undefined)
.then(() => this.getAthleteData().bind(this))
.catch(e => console.log(e));
打电话
this.setState({...})
也返回相同的错误。您的函数似乎没有使用bind或fat arrow(=>)绑定到上下文。您的函数似乎没有使用bind或fat arrow(=>)绑定到上下文。您不认为应该这样调用getAthleteData()吗。getAthleteData()
删除训练(id,e){
e、 预防默认值();
fire.database().ref(“运动员”)
.child(id).remove()//this.getAthleteData())//而不是getAthleteData()
.catch(e=>console.log(e));
}
我可能错了你不认为应该这样调用getAthleteData().getAthleteData()吗
删除训练(id,e){
e、 预防默认值();
fire.database().ref(“运动员”)
.child(id).remove()//this.getAthleteData())//而不是getAthleteData()
.catch(e=>console.log(e));
}
我可能错了有几个问题 1) 如果要调用类的函数,应使用此关键字 2) 要能够调用此函数,必须将函数绑定到正确的位置。要解决这个问题,有几种方法
this.deleteOutwork=this.deleteOutwork.bind(this)代码>
this.deleteOutwork.bind(this)
deleteWorkout = (id, e) => {
e.preventDefault();
fire.database().ref("athlete")
.child(id).remove();
.then(() => this.getAthleteData())
.catch(e => console.log(e));
}
有几个问题 1) 如果要调用类的函数,应使用此关键字 2) 要能够调用此函数,必须将函数绑定到正确的位置。要解决这个问题,有几种方法
this.deleteOutwork=this.deleteOutwork.bind(this)代码>
this.deleteOutwork.bind(this)
deleteWorkout = (id, e) => {
e.preventDefault();
fire.database().ref("athlete")
.child(id).remove();
.then(() => this.getAthleteData())
.catch(e => console.log(e));
}
您在哪里定义了此函数?您需要绑定
deleteWorkout
function是的,很抱歉,它已经绑定了它(在构造函数和内联中都尝试过,就像在编辑后的问题中一样)。。。getAthleteData()函数与DeleteOutwork()在同一个组件中定义。您在哪里定义此函数?您需要绑定DeleteOutwork
function是的,很抱歉,它已经绑定了它(在构造函数和内联中都尝试过,就像在编辑后的问题中一样)。。。getAthleteData()函数与我知道的deleteWorkout()在同一个组件中定义。。。我试着用self=这个绑定它;我尝试在构造函数this.getAthleteData=this.getAthleteData.bind(this)和内联中绑定它,如示例中所示。。。所有的结果都是一样的,我知道。。。我试着用self=这个绑定它;我尝试在构造函数this.getAthleteData=this.getAthleteData.bind(this)和内联中绑定它,如示例中所示。。。所有这些都会产生相同的错误你是对的。。。我会纠正它,但它仍然不起作用。。。我会纠正它,但它仍然不起作用:/Ahhh。。。我没有绑定deleteWorkout函数,只有getAthleteData()函数。谢谢你,伙计!啊。。。我没有绑定deleteWorkout函数,只有getAthleteData()函数。谢谢你,伙计!
deleteWorkout = (id, e) => {
e.preventDefault();
fire.database().ref("athlete")
.child(id).remove();
.then(() => this.getAthleteData())
.catch(e => console.log(e));
}