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 badges
Add a ment  | 

3 Answers 3

Reset to default 5

In 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