[英]How to get values of a field in Firestore in React
I use a document in firestore that stores three fields with boolean values.我在 firestore 中使用了一个文档,该文档存储了三个具有 boolean 值的字段。 This boolean values I need for further processsing.
这个 boolean 值我需要进一步处理。 In my programm I want to read this values.
在我的程序中,我想读取这个值。 But how to read the fields of a document?
但是如何读取文档的字段呢? As explained here it should work like following:
正如这里所解释的,它应该像下面这样工作:
const getStateEntries = async () => {
await getDoc(doc(db, 'Collection_Name', 'Document_ID'))
.then((docSnap)=> {
let alertField = docSnap.data['alert_state']
console.log(alertField)
})
setAlertState(alertField)
}
When I run the code it looks like get no access to the field.当我运行代码时,它看起来无法访问该字段。 When I print the variable to the console, I get an
'undefined'
.当我将变量打印到控制台时,我得到一个
'undefined'
。
The data is not a property on DocumentSnapshot.数据不是 DocumentSnapshot 上的属性。 Try refactoring the code as shown below:
尝试重构代码,如下所示:
let alertField = docSnap.data().alert_state
You can get something like below code:你可以得到类似下面的代码:
firestore()
.collection('Users')
.get()
.then(querySnapshot => {
console.log('Total users: ', querySnapshot.size);
querySnapshot.forEach(documentSnapshot => {
console.log('User ID: ', documentSnapshot.id, documentSnapshot.data());
});
});
For reference use, this one: https://rnfirebase.io/firestore/usage供参考使用,这个: https://rnfirebase.io/firestore/usage
Hope it will help you!希望对您有所帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.