如何使用 Javascript 按文档 Id 对 Firestore 中的数据进行排序?

13

使用JavaScript按文档ID在Firestore中获取排序数据

在Android中,我可以使用查询来访问特定文档

mQuery = docRef.whereEqualTo("name", name)
                .whereEqualTo("valid",true)
                .orderBy(FieldPath.documentId())
                .startAt(lastDocId)
                .limit(LIMIT);

它可以工作,但在Javascript中,我尝试复制一个类似的查询,它没有起作用。

var db = firebase.firestore();
    var goQuery = docRef.where("name", "==", name)
                        .where("valid", "==", true)
                        .orderBy(db.FieldPath.documentId())
                        .startAfter("id0070")
                        .limit(LIMIT);

它会出现错误:

未捕获的 TypeError:无法读取未定义的属性“documentId” 在 HTMLButtonElement.document.getElementById.onclick 中

有任何想法吗?谢谢您的帮助。


1
为什么一个是 FieldPath.documentId(),而另一个是 db.FieldPath.documentId() - epascarello
回复epascarello:因为FieldPath.documentId()会出现错误:Uncaught ReferenceError: FieldPath未定义。 - Jack Wilson
@MikeLin 我觉得你没有理解他的意思。 其实就是为什么第一个被称为 db.FieldPath 而另一个是 FieldPath - choz
1个回答

25

通过不断尝试,正确的语法是:

var goQuery = docRef.where("name", "==", name)
                    .where("valid", "==", true)
                    .orderBy(firebase.firestore.FieldPath.documentId())
                    .startAfter("id0070")
                    .limit(LIMIT);

firebase.firestore.FieldPath.documentId()


3
我该如何在 Node.js 环境中使用它而不使用 Firebase? 我尝试了 db.FieldPath.documentId(),但它会出现错误 undefined - aspirinemaga
你解决了吗? - Shivam Sahil

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接