admin管理员组文章数量:1194923
So, I have a problem that can probably be super easily solved I just can't quite figure it out. Essentially at this point, I'm trying to store fields of a specific document into 2 vars so that I can use them elsewhere.
This is my firestore hierarchy:
This is the code I have so far and I think I'm on the right track but I don't know what to replace "//What do I put here" with.
var db = firebase.firestore();
var user = firebase.auth().currentUser;
var usersEmail = user.email;
db.collection("users").where("email", "==", usersEmail)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
var firstName = //What do I put here?
var lastName = //What do I put here?
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
So, I have a problem that can probably be super easily solved I just can't quite figure it out. Essentially at this point, I'm trying to store fields of a specific document into 2 vars so that I can use them elsewhere.
This is my firestore hierarchy:
This is the code I have so far and I think I'm on the right track but I don't know what to replace "//What do I put here" with.
var db = firebase.firestore();
var user = firebase.auth().currentUser;
var usersEmail = user.email;
db.collection("users").where("email", "==", usersEmail)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
var firstName = //What do I put here?
var lastName = //What do I put here?
});
})
.catch(function(error) {
console.log("Error getting documents: ", error);
});
Share
Improve this question
edited Sep 23, 2018 at 2:12
JWolfCrafter
asked Sep 23, 2018 at 1:07
JWolfCrafterJWolfCrafter
1571 gold badge2 silver badges9 bronze badges
2 Answers
Reset to default 17doc.data()
is just a regular JavaScript object with the contents of the document you just read:
var data = doc.data();
var firstName = data.first;
var lastName = data.last;
Or you can get the field value directly from the DocumentSnapshot
:
var firstName = doc.get("first");
var lastName = doc.get("last");
本文标签: javascriptGetting a specific field in a document returned from firebase firestoreStack Overflow
版权声明:本文标题:javascript - Getting a specific field in a document returned from firebase firestore - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738442827a2087048.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论