[英]How to get value from key from firestore document
I'm trying to read the values from a firestore document, but I'm having issues.我正在尝试从 firestore 文档中读取值,但我遇到了问题。 (React Native Expo managed workflow)
(React Native Expo 托管工作流)
code snippet:代码片段:
let db = firebase.firestore();
//let ref = db.collection(this.props.type).doc(this.props.phone).get();
let ref = db.collection("instructors").doc("0987654321")
let doc = await ref.get();
console.log("setStateFromFirestore");
console.log("data", doc.data()); //this prints fine
data object:数据 object:
"name": "blah"
“名称”:“废话”
"age": "99"“年龄”:“99”
How to get the age or name from such data object?如何从此类数据 object 中获取年龄或姓名?
I've tried these but they don't work:我已经尝试过这些,但它们不起作用:
console.log("data", doc.data().get("age"));
console.log("data", doc.data("age"));
console.log("data", doc.data()["age"]);
What's the way to do this?这样做的方法是什么?
Thanks谢谢
You have two options.你有两个选择。
As a property on the plain JavaScript object returned by data() :作为data()返回的普通 JavaScript object 的属性:
const data = doc.data()
const age = data.age
console.log(age)
Or directly from the DocumentSnapshot using get() :或者直接从 DocumentSnapshot 使用get() :
const age = doc.get("age")
console.log(age)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.