Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/33.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js OrderBy和StartAt具有两个不同的字段firestore_Node.js_Google Cloud Firestore_Pagination - Fatal编程技术网

Node.js OrderBy和StartAt具有两个不同的字段firestore

Node.js OrderBy和StartAt具有两个不同的字段firestore,node.js,google-cloud-firestore,pagination,Node.js,Google Cloud Firestore,Pagination,在我的应用程序中,我的评论字段值为threadCommentCount。我想使用OrderByThreadCommentCount降序排列注释,然后使用startAfter(lastThreadCommentCount)继续分页。问题是,当threadCommentCount为0时(其中有很多),它每次都会返回相同的数据,因为它每次都从0开始。以下是查询: popularCommentsQuery = db .collection('comment

在我的应用程序中,我的评论字段值为threadCommentCount。我想使用OrderByThreadCommentCount降序排列注释,然后使用startAfter(lastThreadCommentCount)继续分页。问题是,当threadCommentCount为0时(其中有很多),它每次都会返回相同的数据,因为它每次都从0开始。以下是查询:

popularCommentsQuery = db
                        .collection('comments')
                        .where('postId', '==', postId)
                        .orderBy('threadCommentCount', 'desc')
                        .startAfter(startAfter)
                        .limit(15)
                        .get()

一旦threadComment计数为0,每次都将返回相同的注释。我无法发送最后一个文档快照,因为我正在使用云函数,我不想在get查询参数中发送文档快照。我不知道;threadCommentCount为0后,我不在乎注释的顺序,我只需要不获取任何重复的注释。任何帮助都是伟大的

所有Firestore查询都有一个
orderBy(“\uuuu name\uuuuu”,direction)
来解析文档之间的任何关联,这些文档对于其他命名的
orderBy
字段具有相同的值。这使得最终排序顺序稳定。但它还允许您将另一个参数传递到
startAfter
,以提供您希望用于分页的锚定文档的文档ID

.startAfter(lastThreadCommentCount, lastDocumentId)

在这两个值之间,您应该能够在结果集中唯一地标识文档以开始下一页。

所有Firestore查询都有一个
orderBy(“\uuuu name\uuuu”,direction)
来解析文档之间的任何关联,这些文档对于其他命名的
orderBy
字段具有相同的值。这使得最终排序顺序稳定。但它还允许您将另一个参数传递到
startAfter
,以提供您希望用于分页的锚定文档的文档ID

.startAfter(lastThreadCommentCount, lastDocumentId)

在这两个值之间,您应该能够在结果集中唯一地标识文档以开始下一页。

因此,我尝试在firestore中使用OrderBy和StartAfter两个不同的字段(时间、键)在flastList中建立分页

关键是我们可以通过文档快照来定义查询光标

下面是我如何做到这一点的

步骤1:使用where()获取文档Id(由firebase自动生成)

步骤2:使用firebase生成的文档Id获取文档快照(我们在第一步中获得)

步骤3:将步骤2中获得的快照传递到startAfter(),以便光标将指向那里


您能改进解决方案吗???

因此,我尝试使用firestore中的OrderBy和StartAfter两个不同字段(时间、键)在flastList中建立分页

关键是我们可以通过文档快照来定义查询光标

下面是我如何做到这一点的

步骤1:使用where()获取文档Id(由firebase自动生成)

步骤2:使用firebase生成的文档Id获取文档快照(我们在第一步中获得)

步骤3:将步骤2中获得的快照传递到startAfter(),以便光标将指向那里


能否改进解决方案???

指定的光标值太多。指定的值必须与查询的orderBy()约束匹配。我是否必须添加另一个orderBy,如果是,在哪里?如果threadCommentCount相同,我想先按threadCommentCount排序,然后按commentId排序。当然,请尝试在第一个之后显式地在_name___上添加orderBy。这样看起来像这样吗?PopularCommentSquary=db.collection('comments')。其中('postId','==',postId')。orderBy('threadCommentCount','desc')。orderBy('commentId','desc')。startAfter(startAfter,lastCommentId).limit(15)或者我真的在第二个orderBy字段中添加了“\uuuuu\name\uuuuuuuu”吗?Nvm,我脑海中突然出现了一个想法,我可以将commentId传递到后端,然后从那里获取快照并传递到startAfter。非常感谢。指定的游标值太多。指定的值必须与查询的orderBy()约束匹配。我是否必须添加另一个orderBy,如果是,在哪里?如果threadCommentCount相同,我想先按threadCommentCount排序,然后按commentId排序。当然,请尝试在第一个之后显式地在_name___上添加orderBy。这样看起来像这样吗?PopularCommentSquary=db.collection('comments')。其中('postId','==',postId')。orderBy('threadCommentCount','desc')。orderBy('commentId','desc')。startAfter(startAfter,lastCommentId).limit(15)或者我真的在第二个orderBy字段中添加了“\uuuuu\name\uuuuuuuu”吗?Nvm,我脑海中突然出现了一个想法,我可以将commentId传递到后端,然后从那里获取快照并传递到startAfter。非常感谢。
const docRef2= firestore().collection('shots').doc(fbDocIdGeneratedByFirebase)
const snapshot = await docRef2.get();
 let additionalQuery =  firestore().collection('shots')
         .orderBy("time", "desc")
         .startAfter(snapshot)
         .limit(this.state.limit)      
 let documentSnapshots = await additionalQuery.get(); // you know what to do next
...