MarioPhysics
MarioPhysics

Reputation: 11

Fetching data from Firebase Firestore

Javascript. This works:

var snapshot = db.collection('xyz').doc("ABCD").get().then((snapshot) => {...

the omitted code delivers the content of ABCD to the console.log but this doesn't:

var snapshot = db.collection("xyz").where("ownkey","==","ABCD").get().then((snapshot) => {...}

A snapshot.exists test fails. I've checked the obvious, the case of the letters in ownkey agrees with the document and I'm stuck. Please help.

Upvotes: 0

Views: 202

Answers (1)

Siratee K.
Siratee K.

Reputation: 197

The instance of object that will be returned from those 2 code are different.

  1. By using .doc()

The value that will be returned is the instance of DocumentSnapshot which contain the data of the single document you request.


  1. By using .where()

This means you want to search something and it will return the data as the instance of QuerySnapshot. which can contain 0 or more of the document data like an array. (This instance doesn't contain .exists property so this might failed your test)

So if you want to check that the search found something (Contain 1 or more of the document data), you can use .empty to check it

snapshot.empty

or if you want to retrieve the data from it, you may need to use .forEach() to loop all the data out from that instance

snapshot.forEach(docSnap => {
  // docSnap is like a normal DocumentSnapshot.
})

Upvotes: 2

Related Questions