Pacholoamit
Pacholoamit

Reputation: 265

Firestore returning "undefined"

I think I'm missing something. I'm trying to get data from my firestore database. I'm sure the collection field is correct, the doc field is correct up until the console.log. When I console.log(credits) it returns a firestore snapshot. However, if I do console.log(credits.data()) then I get returned undefined. What am I missing?

  useEffect(() => {
    async function getCredits() {
      await db.collection('users_portfolio').doc(currentUser.email).get().then(credits => {
        console.log(credits.data()
      }).catch(error => {
        console.log(error)
      })
    } getCredits()
  }, [db, currentUser.email])

Upvotes: 0

Views: 2388

Answers (1)

Louis Coulet
Louis Coulet

Reputation: 4641

When doc.data() returns undefined, it means that your document does not exist. You can verify it this way:

if(credits.exist) {
  console.log(credits.data());
}
else {
  console.log(`${credits.ref} does not exist`);
}

More info in the reference

Upvotes: 1

Related Questions