【发布时间】:2018-11-28 03:03:16
【问题描述】:
我的数据按预期显示在 console.log 上,但不在我的屏幕上。 这是控制台日志的屏幕截图。
这是我的代码:
componentWillMount = () => {
this.getData();
}
getData(){
const { currentUser } = firebase.auth();
firebase
.database()
.ref(`/users/${currentUser.uid}/data/`)
.orderByKey()
.on('child_added', snap => {
//Is this correct or does it need to be formatted a different way?
snap.key,
snap.val().Weight
//note that the console logs below displays the data
console.log(snap.key)
console.log(snap.val().Weight)
})
}
renderRow = () => {
return (
<View style={[styles.card, styles.cardBorderTop]}>
<Text>
{snap.key} //Is this the correct way to reference this value?
</Text>
<Text style={[styles.textRight]}>
{snap.val().Weight} //Is this the correct way to reference this value?
</Text>
</View>
)
}
render() {
return (
<View style={[styles.container]}>
<FlatList
data={this.getData} //Is this the correct data reference?
renderItem={this.renderRow}
/>
</View>
);
}
}
这就是我的屏幕呈现方式。请注意,我希望数据呈现在 FlatList 上。
任何帮助将不胜感激。
附带说明一下,我现在意识到我需要将日期存储为 ISO-8601,以便它们可以正确排序,我将在弄清楚如何获取查询数据以呈现在我的屏幕。
更新 我意识到我的问题并不像我预期的那么清楚,对此我深表歉意。我需要的是能够通过 date key 和 Weight child 查询我的数据。我可以在控制台上使用 snap.key 和 snap.val().Weight 成功地做到这一点,但是看起来这不是在我的 FlatList 上显示数据所需的正确参考,这就是我需要帮助的地方.
【问题讨论】:
-
数据显示方式有什么问题?
-
在控制台日志上很好,但它没有使用 FlatList 显示在我的屏幕上。我更新了原始帖子以包含我的空白屏幕。我的代码中遗漏了一些东西。查看 renderRow 函数和 FlatList 数据参考。有点不对劲。
-
我真的不太确定这应该做什么:
{ snap.key, snap.val().Weight console.log(snap.key + snap.val().Weight + 'LBS') }。这应该返回给调用者什么?你觉得你的来电者在这里data={this.getData}是如何使用它的? -
snap.key 按控制台日志中的预期返回我的密钥。例如:FRI NOV 16 2018 MON NOV 12 2018 等...并且 snap.val().Weight 还按预期返回 Weight 子数据。例如: 175 171 但是,看起来这不是能够在平面列表上显示我的数据所需的正确代码。它是否正确? .on('child_added', snap => { snap.key, snap.val().Weight })
标签: javascript firebase react-native firebase-realtime-database