2

I cannot seem to get any docs from firebase in my react-native project.

This is the code:

async function GetUsers() {
    firebase.firestore()
        .collection("Users")
        .get()
        .then(function (querySnapshot) {
            console.log(querySnapshot);
            querySnapshot.docs.forEach(function (doc) {
                console.log(5);
            });
        });
}

The function is called in

componentDidMount() {
        GetUsers();
    }

But the console is never fired because the docs is always empty.

Here's my Database Structure

The connection is definitely setup correctly because I can add docs.

Here's the output querySnapshot

What am I doing wrong?

Harry Feld
  • 73
  • 1
  • 6
  • try removing await before the collection retrieval and verify. – Yakub Pasha Apr 10 '20 at 06:49
  • How do you call this? Can you show the context? – Renaud Tarnec Apr 10 '20 at 07:24
  • You shouldn't be using `await` in the same function call as `then()`, however the `get()` should still be getting executed anyway. Could you share some more code so we can check if there are other problems before this function call? – Ralemos Apr 10 '20 at 09:22
  • I've added a bit more code to the original post, it all runs pretty clean up until the .then() – Harry Feld Apr 10 '20 at 12:20
  • I've removed the await but that doesnt seem to make any changes – Harry Feld Apr 10 '20 at 12:22
  • Try to replace your `querySnapshot.forEach` to `querySnapshot.docs.forEach`, let me know what comes up, also, try to print your `querySnapshot` on console to check whether it is entering on the `.then()` – Ralemos Apr 10 '20 at 12:50
  • I've changed the code again (check edited post). It's returning the querySnapshot but '5' is definitely never called – Harry Feld Apr 11 '20 at 06:53
  • This is very strange, cause you code does seems to be correct, the only explaination for what is happening is that there are no users, which is not the case. Add this to your code: `if (!querySnapshot.hasData || querySnapshot.data.documents.isEmpty) { console.log("empty snapshot"); }` and if it does log the empty snapshot text, it will mean that there is something wrong while performing the `.get()` – Ralemos Apr 14 '20 at 14:33
  • Also take a look at this [accepted community answer](https://stackoverflow.com/questions/52880387/fetching-data-from-firestore/52880460#52880460), it might be something applicable to your issue – Ralemos Apr 14 '20 at 14:38
  • @ralemos If added your code and it does indeed return "Empty Snapshot". Where on earth do I go from here? – Harry Feld Apr 18 '20 at 07:51
  • Remove the `async` from the `getUsers` function, that will make it all run syncrounously. – Ralemos Apr 22 '20 at 15:09
  • Still nothing. I can see the querySnapshot but docs must be an empty array because `console.log(5)` is never called – Harry Feld Apr 23 '20 at 23:55
  • I've added a snippet of the querySnapshot if that helps – Harry Feld Apr 24 '20 at 00:02
  • I'm guessing it also has something to do with this message that I keep getting: `Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.` But I have no idea how to get rid of it. – Harry Feld Apr 24 '20 at 00:20
  • This might be related, as you can see on this [documentation](https://firebase.google.com/support/release-notes/js#cloud-firestore_65), this error indicates that you are using cached data (which is not filled since you never populated it) since the connection is failing. Try the solutions on this [community question](https://stackoverflow.com/questions/50674482/firebase-firestore-firestore-5-0-4-could-not-reach-cloud-firestore-backend) and let me know if it works. – Ralemos Apr 24 '20 at 13:34
  • 1
    Couldn't seem to get this working. I've swapped to pouchDB hosted from IBMCloud and it worked pretty much immediately. Thanks for the effort though. – Harry Feld Apr 29 '20 at 11:23
  • Anytime, by the way, you might want add your solution as an answer to this questions, so that if someone in the community gets the same issue, they find your solution easily and also to include your reputation. – Ralemos Apr 30 '20 at 09:18

0 Answers0