【问题标题】:Firestore startAfter method is not working with a document as a referenceFirestore startAfter 方法无法将文档用作参考
【发布时间】:2020-08-05 22:39:59
【问题描述】:

我遇到了这个问题,我无法让 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 .....

【问题讨论】:

  • 在 Stack Overflow 上,不要显示代码图片。将代码复制到问题中并设置格式,以便于阅读、复制和搜索。
  • @DougStevenson 抱歉,已修复。希望它现在更具可读性

标签: javascript firebase google-cloud-firestore


【解决方案1】:

这个问题的解决方案是我获取的是数据而不是 doc 参考。

要修复这样的问题,您必须在代码中添加这样的内容

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

【讨论】:

    【解决方案2】:

    实际上第一个代码块运行的原因是因为那是startAt()的正确用法。

    正如您在Official Documentation 中的示例中看到的那样,您应该使用startAt() 中的值而不是完整的文档,如果您认为您正在按特定字段对数据进行排序并且您还应该以同一字段上的特定值开始您的结果。

    所以在你的情况下,正确的用法确实是.startAfter(last.createdAt)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-03-25
      • 1970-01-01
      • 2019-06-14
      • 1970-01-01
      • 2019-08-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多