[英]Firestore retrieve a single document from a collection based on orderBy and limit without forEach
My Javascript looks similar to this:我的 Javascript 看起来与此类似:
firestoredb.collection('abc')
.doc('bcd')
.collection('cde')
.orderBy('bid', 'desc')
.orderBy('timestamp', 'asc')
.limit(1)
.get()
.then(function(querySnapshot) {
querySnapshot.forEach(function(doc) {
console.log(doc.data().bid);
});
});
Is it possible to retrieve doc.data().bid
without the forEach
statement and maybe without the querySnapshot
variable?是否可以在没有
forEach
语句并且可能没有querySnapshot
变量的情况下检索doc.data().bid
? Since I know there will only be one record in this case.因为我知道在这种情况下只会有一个记录。 Or is
forEach
still the recommended way to do this?还是
forEach
仍然是推荐的方法? Everything else I have tried results in an error message.我尝试过的所有其他方法都会导致错误消息。 I am mostly wondering if there is a simpler way to code this.
我主要想知道是否有更简单的方法来编码。
Couldn't fit the answer into a comment.无法将答案放入评论中。 Is this what you were looking for?
这是你要找的吗?
firestoredb.collection('abc')
.doc('bcd')
.collection('cde')
.orderBy('bid', 'desc')
.orderBy('timestamp', 'asc')
.get()
.then(querySnapshot => console.log(querySnapshot.docs[0].data().bid));
I hope the code below helps.我希望下面的代码有所帮助。
firestoredb.collection('abc')
.doc('bcd')
.collection('cde')
.orderBy('bid', 'desc')
.orderBy('timestamp', 'asc')
.limit(1)
.get()
.then(function(doc) {
console.log(doc.data().bid);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.