[英]How do I retrieve a field value from Firebase using React?
I am trying to learn Firebase. 我正在尝试学习Firebase。 I made a very simple ReactApp along with a very simple database in Firebase. 我在Firebase中制作了一个非常简单的ReactApp和一个非常简单的数据库。 I am so beyond confused as to how to access a field's value. 关于如何获取字段的值,我感到非常困惑。 I'm sure this is an extremely simple question, but somehow, I am missing how to find the solution. 我敢肯定这是一个非常简单的问题,但是不知何故,我错过了如何找到解决方案。
My database looks as such: https://imgur.com/a/3LczlwI 我的数据库如下所示: https : //imgur.com/a/3LczlwI
I really would like to know how to access the "Speed" field value. 我真的很想知道如何访问“速度”字段值。
I have tried 我努力了
const db = firebase.database.ref();
const speedRef =
db.child('react').child('mySpeed').once('value').then(snap =>{
if (snap.val()){
this.setState({ speed: snap.val().speed });
} else {
console.log('error');
}
})
Along with 随着
const speedRef = firebase.database().ref().child('mySpeed').child('speed');
speedRef.on('value', snap => {
this.setState({ speed: snap.val() });
})
To no avail. 无济于事。 I really hope someone can help me understand this. 我真的希望有人能帮助我理解这一点。 Thank you in advance. 先感谢您。
*try to use this: * *尝试使用此功能:*
const react = firebase.database().ref("react"); react.child("mySpeed").child("speed").once("value").then(data => { if(data.val() !== null){ console.log(data.val()); } });
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.