Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
126 views
in Technique[技术] by (71.8m points)

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.

question from:https://stackoverflow.com/questions/66059670/fetching-data-from-firebase-firestore

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...