admin管理员组文章数量:1333435
I am not able to pull data on collection, getting this error
Uncaught TypeError: doc.data is not a function
var db = firebase.firestore();
const docRef = db.collection("Slides");
getRealData = function() {
docRef.onSnapshot(function(doc) {
const myData = doc.data();
console.log(myData);
});
};
getRealData();
I find the solution if I pass fix id then below code work (Although ID are random generate by firestore)
db.collection("Slides").doc("GYUzWG6jcOPob725wbnF")
.onSnapshot(function(doc) {
console.log("Current data: ", doc.data());
});
Firestore
I am not able to pull data on collection, getting this error
Uncaught TypeError: doc.data is not a function
var db = firebase.firestore();
const docRef = db.collection("Slides");
getRealData = function() {
docRef.onSnapshot(function(doc) {
const myData = doc.data();
console.log(myData);
});
};
getRealData();
I find the solution if I pass fix id then below code work (Although ID are random generate by firestore)
db.collection("Slides").doc("GYUzWG6jcOPob725wbnF")
.onSnapshot(function(doc) {
console.log("Current data: ", doc.data());
});
Firestore
Share edited Mar 15, 2019 at 6:00 Doug Stevenson 318k36 gold badges455 silver badges473 bronze badges asked Mar 15, 2019 at 5:51 faisaljanjuafaisaljanjua 9363 gold badges13 silver badges29 bronze badges3 Answers
Reset to default 5In your first code sample, you're assuming that the snapshot callback attached to this:
db.collection("Slides")
Is similar to the one attached to this:
db.collection("Slides").doc("GYUzWG6jcOPob725wbnF")
In fact, they are not the same at all.
The first one will query for ALL the documents in the named collection, and it will give you a QuerySnapshot object in the callback. This object does not have a data() method, and you need to iterate it to get all the document snapshots.
The second one will query for only the named document in the named collection, and you will get a DocumentSnapshot object back.
Since you didn't say what you're trying to acplish, I can't remend what you should be doing. All I can say is that the two code bits you've shown are not at all alike, and you'd expect them to behave differently.
By the error message looks like data is not a function, it mean that you don't have to use parentheses when assigning it to variable, so change your assignment to this line:
const myData = doc.data;
Actually, my approach was wrong, use get
function to get all the data.
docRef.get().then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
// doc.data() is never undefined for query doc snapshots
console.log(doc.id, " => ", doc.data());
});
});
本文标签: javascriptFirstore data is not functionStack Overflow
版权声明:本文标题:javascript - Firstore data is not function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742293449a2448250.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论