The instance of object that will be returned from those 2 code are different.
- By using
.doc()
The value that will be returned is the instance of DocumentSnapshot
which contain the data of the single document you request.
- 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.
})
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…