Javascript 如何使用React访问Firebase中的嵌套数据?
我正在使用React和Firebase构建一个简单的应用程序,这是一种管理收入和支出的方法,我的Firebase采用以下结构:Javascript 如何使用React访问Firebase中的嵌套数据?,javascript,reactjs,firebase,Javascript,Reactjs,Firebase,我正在使用React和Firebase构建一个简单的应用程序,这是一种管理收入和支出的方法,我的Firebase采用以下结构: "cashbook" : { "expenditure" : { "expenditure-1" : { "amount" : "500", "category" : "insurance", "date" : "date", "name" : "Life Insurance", "type" :
"cashbook" : {
"expenditure" : {
"expenditure-1" : {
"amount" : "500",
"category" : "insurance",
"date" : "date",
"name" : "Life Insurance",
"type" : "recurring"
}
},
"income" : {
"salary" : {
"amount" : "500",
"category" : "salary",
"date" : "date",
"type" : "recurring"
}
}
}
我已使用以下方法,仅使用支出,完美地设置和工作:
然后我可以使用:this.state.expensation
然而,当我尝试扩展应用程序来访问收入数据时,事情就变得一团糟了。我将组件DIDMOUNT修改为:
componentDidMount: function() {
// Two way data binding
base.syncState('cashbook', {
context: this,
state: 'cashbook'
});
},
并尝试使用this.state.cashbook.expression
和this.state.cashbook.income
访问,但没有乐趣,我得到错误:未捕获类型错误:无法读取未定义的属性“expression”
不太确定要尝试什么,任何指针都是神圣的棕色
提前谢谢:)原来我是个白痴:)
我只需要在cashbook
上使用syncstate,而不是cashbook/expense
,然后调用this.state.cashbook.expense
当您尝试访问这些东西时会发生什么?您是否收到特定的错误消息?如果您使用React developer工具查看组件的状态,您会看到什么?抱歉。更新了主帖子,但错误是uncaughttypeerror:无法在此处读取未定义的
Re-base创建者的属性“支出”。这听起来可能是Re-base的问题,因为您的代码看起来很好。你介意在这个项目上制造一个问题,我来看看吗?嗨,泰勒,抱歉,圣诞节来了!Re-base没有问题,我只需要在cashbook
上使用syncstate,而不是cashbook/expense
,然后调用this.state.cashbook.expense
。
componentDidMount: function() {
// Two way data binding
base.syncState('cashbook', {
context: this,
state: 'cashbook'
});
},