Reactjs Firebase数据库抛出“;错误:Reference.set失败;
我试图将一组数据推送到我的firebase数据库,但一直返回错误。我一直在尝试不同的修复方法,但没有一种有效 这是错误页面的图片: 如果有人能帮助我,我将永远感激:) 代码:Reactjs Firebase数据库抛出“;错误:Reference.set失败;,reactjs,firebase,firebase-realtime-database,Reactjs,Firebase,Firebase Realtime Database,我试图将一组数据推送到我的firebase数据库,但一直返回错误。我一直在尝试不同的修复方法,但没有一种有效 这是错误页面的图片: 如果有人能帮助我,我将永远感激:) 代码: class App extends Component { constructor(props){ super(props); this.addOpportunity = this.addOpportunity.bind(this); this.removeOpportunity = this.
class App extends Component {
constructor(props){
super(props);
this.addOpportunity = this.addOpportunity.bind(this);
this.removeOpportunity = this.removeOpportunity.bind(this);
this.app = firebase.initializeApp(DB_CONFIG);
this.database = this.app.database().ref().child('opportunities');
//We are going to setup the React state of our component
this.state = {
opportunities: [],
}
}
componentWillMount(){
const previousOpportunities = this.state.opportunities;
// Data Snapshot
this.database.on('child_added', snap => {
previousOpportunities.push({
id: snap.key,
opportunityTitle: snap.val(),
opportunityCompany: snap.val().opportunityCompany,
opportunityLocation: snap.val().opportunityLocation,
opportunityDescription: snap.val().opportunityDescription,
opportunityLink: snap.val().opportunityLink,
})
this.setState({
opportunities: previousOpportunities
})
})
this.database.on('child_removed', snap => {
for(var i=0; i < previousOpportunities.length; i++){
if(previousOpportunities[i].id === snap.key){
previousOpportunities.splice(i, 1);
}
}
this.setState({
opportunities: previousOpportunities
})
})
}
addOpportunity(){
this.database.push().set({opportunityTitle: this.state.newopportunityTitle,
opportunityCompany: this.state.newopportunityCompany,
opportunityLocation: this.state.newopportunityLocation,
opportunityDescription: this.state.newopportunityDescription,
opportunityLink: this.state.newopportunityLink});
}
类应用程序扩展组件{
建造师(道具){
超级(道具);
this.addOpportunity=this.addOpportunity.bind(this);
this.removeOpportunity=this.removeOpportunity.bind(this);
this.app=firebase.initializeApp(DB_CONFIG);
this.database=this.app.database().ref().child('opportunities');
//我们将设置组件的反应状态
此.state={
机会:[],
}
}
组件willmount(){
const previousOpportunities=this.state.opportunities;
//数据快照
this.database.on('child_added',snap=>{
以前的机会({
id:snap.key,
opportunityTitle:snap.val(),
机会主义公司:snap.val()。机会主义公司,
opportunityLocation:snap.val().opportunityLocation,
opportunityDescription:snap.val().opportunityDescription,
opportunityLink:snap.val().opportunityLink,
})
这是我的国家({
机遇:以前的机遇
})
})
this.database.on('child_removed',snap=>{
对于(var i=0;i
我想你想要的是这个.state.opportunityTitle
而不是这个.state.newopportunityTitle
非常感谢Colin!当我做以下事情时,它似乎起了作用:
addOpportunity(newOpportunityTitle, newopportunityCompany,
newopportunityLocation, newopportunityDescription, newopportunityLink){
this.database.push({opportunityTitle: newOpportunityTitle,
opportunityCompany: newopportunityCompany,
opportunityLocation: newopportunityLocation,
opportunityDescription: newopportunityDescription,
opportunityLink: newopportunityLink});
}你能发布你的代码吗?应该现在添加,非常感谢@ColinThanks Colin!当我执行以下操作时,它似乎正在工作:addOpportunity(newOpportunityTitle,newopportunityCompany,newopportunityLocation,newopportunityDescription,newopportunityLink){this.database.push({opportunityTitle:newOpportunityTitle,opportunityCompany:newopportunityCompany,opportunityLocation:newopportunityLocation,opportunityDescription:newopportunityDescription,opportunityLink:newopportunityLink});}酷,如果答案有帮助,请投票,或标记为正确。
addOpportunity(newOpportunityTitle, newopportunityCompany,
newopportunityLocation, newopportunityDescription, newopportunityLink){
this.database.push({opportunityTitle: newOpportunityTitle,
opportunityCompany: newopportunityCompany,
opportunityLocation: newopportunityLocation,
opportunityDescription: newopportunityDescription,
opportunityLink: newopportunityLink});