gpt4 book ai didi

javascript - Firestore startAfter 方法无法将文档用作引用

转载 作者:行者123 更新时间:2023-12-03 23:04:59 25 4
gpt4 key购买 nike

我遇到了这个问题,我无法让 startAfter 处理我在 Firestore 中的数据。
我给出了这两个问题的例子,第一个图像工作时,使用属性(createdAt)过滤,第二个传递整个文档返回空并且不能让 forEach 循环遍历数据
有人知道这是怎么回事吗?这些文件没有任何复杂的信息,名称、创建日期、所有数字用于测试。
如果有人遇到这个问题,请帮我解答,前几天刚开始学习 Firebase。
提前致谢 :)

// getting the data
const response = await db
.collection("apis")
.orderBy("createdAt")
.limit(3)
.get();
const dataSend = [];
response.forEach((document) => {
dataSend.push(document.data());
});
//triggering the next data load

const getMore = async () => {
const limit = 3;
const last = apis[apis.length - 1]; // last document
console.log(last); // {name: "3", description: "3", createdAt: t, url: "3", authorId: 123123, …}

try {
const response = await db
.collection("apis")
.limit(limit)
.orderBy("createdAt")
.startAfter(last.createdAt) // passing createdAt to fix the problem
.get();
console.log(response);
const dataSend = [];
response.forEach((document) => {
//this is not entering here
dataSend.push(document.data());
});
} catch .....
第二种情况
// getting the data
const response = await db
.collection("apis")
.orderBy("createdAt")
.limit(3)
.get();
const dataSend = [];
response.forEach((document) => {
dataSend.push(document.data());
});
//triggering the next data load

const getMore = async () => {
const limit = 3;
const last = apis[apis.length - 1]; // last document
console.log(last); // {name: "3", description: "3", createdAt: t, url: "3", authorId: 123123, …}

try {
const response = await db
.collection("apis")
.limit(limit)
.orderBy("createdAt")
.startAfter(last) // PASSING THE WHOLE DOCUMENT AS A PARAMETER DO NOT WORK
.get();
console.log(response);
const dataSend = [];
response.forEach((document) => {
//this is not entering here
dataSend.push(document.data());
});
} catch .....

最佳答案

这个问题的解决方案是我得到的是数据而不是 文档 引用。
要解决这样的问题,您必须在代码中添加类似这样的内容
response.docs[response.docs.length - 1]

// getting the data
const response = await db
.collection("apis")
.orderBy("createdAt")
.limit(3)
.get();
const dataSend = [];
const last = response.docs[response.docs.length - 1] // this is the reference to the doc that the documentations says
response.forEach((document) => {
dataSend.push(document.data());
});
//triggering the next data load

const getMore = async () => {
const limit = 3;
const last = response.docs[response.docs.length - 1] // last document
console.log(last); // {name: "3", description: "3", createdAt: t, url: "3", authorId: 123123, …}

try {
const response = await db
.collection("apis")
.limit(limit)
.orderBy("createdAt")
.startAfter(last) //
.get();
console.log(response);
const dataSend = [];
response.forEach((document) => {
//this is not entering here
dataSend.push(document.data());
});
} catch .....

因此,不是从数据库中传递最后一个对象,而是在使用 Firebase 提供的 data() 函数进行转换之前传递对文档的最后一个引用。
它也比传递 object.createdAt 更好。
引用
https://firebase.google.com/docs/firestore/query-data/query-cursors

关于javascript - Firestore startAfter 方法无法将文档用作引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63274481/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com