Unm*_*my_ 5 javascript firebase google-cloud-firestore
我遇到了这个问题,无法在 Firestore 中使用我的数据进行 startAfter 工作。\n我给出了这个问题的两个示例,第一张图片是在它工作时使用属性(createdAt)进行过滤的,第二次传递整个文档返回空并且无法使 forEach 循环数据
\n有人知道这是怎么回事吗?这些文件没有任何复杂的信息,名称,创建日期,所有用于测试的数字。
\n如果有人遇到这个问题,请帮我解答,我几天前刚刚开始学习 Firebase。\n提前致谢:)
\n// getting the data\nconst response = await db\n .collection("apis")\n .orderBy("createdAt")\n .limit(3)\n .get();\n const dataSend = [];\n response.forEach((document) => {\n dataSend.push(document.data());\n });\n//triggering the next data load\n\n const getMore = async () => {\n const limit = 3;\n const last = apis[apis.length - 1]; // last document\n console.log(last); // {name: "3", description: "3", createdAt: t, url: "3", authorId: 123123,\xc2\xa0\xe2\x80\xa6}\n\n try {\n const response = await db\n .collection("apis")\n .limit(limit)\n .orderBy("createdAt")\n .startAfter(last.createdAt) // passing createdAt to fix the problem\n .get();\n console.log(response);\n const dataSend = [];\n response.forEach((document) => {\n //this is not entering here\n dataSend.push(document.data());\n });\n } catch .....\nRun Code Online (Sandbox Code Playgroud)\n第二种情况
\n// getting the data\nconst response = await db\n .collection("apis")\n .orderBy("createdAt")\n .limit(3)\n .get();\n const dataSend = [];\n response.forEach((document) => {\n dataSend.push(document.data());\n });\n//triggering the next data load\n\n const getMore = async () => {\n const limit = 3;\n const last = apis[apis.length - 1]; // last document\n console.log(last); // {name: "3", description: "3", createdAt: t, url: "3", authorId: 123123,\xc2\xa0\xe2\x80\xa6}\n\n try {\n const response = await db\n .collection("apis")\n .limit(limit)\n .orderBy("createdAt")\n .startAfter(last) // PASSING THE WHOLE DOCUMENT AS A PARAMETER DO NOT WORK\n .get();\n console.log(response);\n const dataSend = [];\n response.forEach((document) => {\n //this is not entering here\n dataSend.push(document.data());\n });\n } catch .....\nRun Code Online (Sandbox Code Playgroud)\n
这个问题的解决方案是我获取数据而不是文档参考。
\n要解决这样的问题,您必须在代码中添加如下内容
\n响应.文档[响应.文档.长度 - 1]
\n// getting the data\nconst response = await db\n .collection("apis")\n .orderBy("createdAt")\n .limit(3)\n .get();\n const dataSend = [];\n const last = response.docs[response.docs.length - 1] // this is the reference to the doc that the documentations says\n response.forEach((document) => {\n dataSend.push(document.data());\n });\n//triggering the next data load\n\n const getMore = async () => {\n const limit = 3;\n const last = response.docs[response.docs.length - 1] // last document\n console.log(last); // {name: "3", description: "3", createdAt: t, url: "3", authorId: 123123, \xe2\x80\xa6}\n\n try {\n const response = await db\n .collection("apis")\n .limit(limit)\n .orderBy("createdAt")\n .startAfter(last) //\n .get();\n console.log(response);\n const dataSend = [];\n response.forEach((document) => {\n //this is not entering here\n dataSend.push(document.data());\n });\n } catch .....\n\nRun Code Online (Sandbox Code Playgroud)\n因此,您不必传递数据库中的最后一个对象,而是在使用 Firebase 提供的 data() 函数进行转换之前传递对文档的最后一个引用。
\n而且它比传递 object.createdAt 效果更好。
\nhttps://firebase.google.com/docs/firestore/query-data/query-cursors
\n| 归档时间: |
|
| 查看次数: |
2437 次 |
| 最近记录: |