最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firestore startAfter method is not working with a document as a reference - Stack Overflow

programmeradmin2浏览0评论

i'm having this problem where I can't make the startAfter work with my data in Firestore. I'm giving this two examples of the issue the first image is when it works, filtering with a property(createdAt), the second passing the whole doc returns empty and can't make the forEach to loop through the data

Does some know what this happen ? the documents do not have any plex information, name, creation date, all numbers for testing.

If someone had this problem, please help me with an answer, just started learning Firebase a few days ago. Thanks in advance :)

// 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 .....

SECOND CASE

// 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 .....

i'm having this problem where I can't make the startAfter work with my data in Firestore. I'm giving this two examples of the issue the first image is when it works, filtering with a property(createdAt), the second passing the whole doc returns empty and can't make the forEach to loop through the data

Does some know what this happen ? the documents do not have any plex information, name, creation date, all numbers for testing.

If someone had this problem, please help me with an answer, just started learning Firebase a few days ago. Thanks in advance :)

// 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 .....

SECOND CASE

// 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 .....
Share Improve this question edited Aug 5, 2020 at 22:51 Unmy_ asked Aug 5, 2020 at 22:39 Unmy_Unmy_ 4851 gold badge6 silver badges18 bronze badges 2
  • 3 On Stack Overflow, don't show pictures of code. Copy the code into the question and format it so that it's easy to read, copy, and search. – Doug Stevenson Commented Aug 5, 2020 at 22:42
  • @DougStevenson i'm sorry, fixed. Hope it's more readable now – Unmy_ Commented Aug 5, 2020 at 22:52
Add a ment  | 

2 Answers 2

Reset to default 5

The solution to this problem was that I was getting the data and not the doc reference.

To fix something like this you'll have to add to the code something like this

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 .....

So instead of passing the last object from the database you pass the last reference to the doc before it transforms with the data() function that Firebase provides.

ALSO it works better than passing the object.createdAt.

REFERENCE

https://firebase.google./docs/firestore/query-data/query-cursors

Actually the reason that the first code block is running is because that is the correct usage of startAt().

As you can see on the examples in the Official Documentation, you should use a value in startAt() and never a full document, and that actually makes sense if you consider that you are sorting the data by a specific field and you should also start your results by a specific value on that same field.

So the correct usage is indeed .startAfter(last.createdAt) in your case.

发布评论

评论列表(0)

  1. 暂无评论