Skip to content

Firestore: No reponse form the getDocs() when used with the query constraints endBefore & limitToLast #6158

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Thanga-Ganapathy opened this issue Apr 15, 2022 · 8 comments
Assignees

Comments

@Thanga-Ganapathy
Copy link

[REQUIRED] Describe your environment

  • Operating System version: Pop!_OS 21.10
  • Browser version: Google Chrome Version 100.0.4896.88 (Official Build) (64-bit)
  • Firebase SDK version: 9.6.11
  • Firebase Product: firestore
  • React 18.0.0

[REQUIRED] Describe the problem

There was no response from the getDocs() function when used with query constraints endBefore & limitToLast.

Earlier, this was working on the Beta(9.0.0-beta.8) version of the SDK. After an upgrade to the latest version, it broke.

My main concern is that while awaiting a response from the getDocs function, it does not throw any errors either.

Steps to reproduce:

  1. Add some data to a collection.
  2. Get the first page using the query with orderBy(name) and limit(5).
  3. Get the next page using the query with startAfter(page1 last cursor), orderBy(name) and limit(5).
  4. Get the prev page using the query with endBefore(page2 first cursor), orderBy(name) and limitToLast(5).
  5. No response or error from the prev page query.

Relevant Code:

async function runTest() {
  const db = getFirestore();
  const ref = collection(db, "items");

  // add test data
  const batch = writeBatch(db);
  [...new Array(10)].forEach((a, i) => {
    batch.set(doc(ref, "Item" + (i + 1)), { name: "Item" + (i + 1) });
  });
  await batch.commit();

  // get first page
  const page1 = await getDocs(query(ref, orderBy("name"), limit(5))).then(
    (snap) => {
      console.log(
        "page 1",
        snap.docs.map((doc) => doc.data().name)
      );
      return snap.docs;
    }
  );

  // get next page
  const page2 = await getDocs(
    query(ref, orderBy("name"), limit(5), startAfter(page1[page1.length - 1]))
  ).then((snap) => {
    console.log(
      "page 2",
      snap.docs.map((doc) => doc.data().name)
    );
    return snap.docs;
  });

  // get prev page
  await getDocs(
    query(ref, orderBy("name"), limitToLast(5), endBefore(page2[0]))
  ).then((snap) => {
    console.log(
      "should be page 1",
      snap.docs.map((doc) => doc.data().name)
    );
    return snap.docs;
  });
}

runTest();

/*
Output: 

page 1 (5) ['Item1', 'Item10', 'Item2', 'Item3', 'Item4']
page 2 (5) ['Item5', 'Item6', 'Item7', 'Item8', 'Item9']
*/
@nwaughachukwuma
Copy link

I can confirm that getDocs doesn't resolve with or without query constraints

@wu-hui
Copy link
Contributor

wu-hui commented Apr 21, 2022

Just want to provide an update: there is another bug reported on endBefore and limitToLast, and it is fixed via: #6168

Although the two reports do not have the same symptom, it is possible that they are from different platforms.

Let's wait to see if the next release did fix the issue. We will look into this again if it does not.

@Thanga-Ganapathy
Copy link
Author

@wu-hui Thanks for the fix.

I have tested this in the firebase@canary. It is working fine and seems to be fixed.

One suggestion: in the future, please ensure that it throws some errors when awaiting a response from the async functions if something goes wrong with some kind of timeout or any other solution. Because when the async functions do not respond, the app exhibits undesirable behaviour.

Please close this issue if it can be closed without any further action needed.

Thanks.

@nwaughachukwuma
Copy link

nwaughachukwuma commented Apr 25, 2022

I just tested firebase": "9.6.11-canary.dc0e92632" and it doesn't seem resolved yet. In the snippet below, the console statement never gets called.

const activeUsers = await getDocs(query(colRef, where('status', '==', 'active')))
console.log('resolved!')

@wu-hui
Copy link
Contributor

wu-hui commented Apr 26, 2022

Hi @Thanga-Ganapathy

Thanks for confirming! I do not know why it manifest as a timeout in your environment, maybe something we should look into.

Hi @nwaughachukwuma

Are you reporting a new issue, or you are experiencing the same issue with endBefore and limitToLast?

It seems like you are reporting a new one, from the snippet you pasted. I am going to close this one, and feel free to open a new ticket for your issue.

@wu-hui wu-hui closed this as completed Apr 26, 2022
@nwaughachukwuma
Copy link

Hi @wu-hui, I considered the issues to be the same or have the same root cause. My observation is that getDocs never resolves when passed a query constraint - in my case, it doesn't resolve with a simple where constraint

const activeUsers = await getDocs(query(colRef, where('status', '==', 'active')))
console.log('resolved!') // <-- never reached

@nwaughachukwuma
Copy link

I can confirm that this now works for me

@mariusschroeter
Copy link

mariusschroeter commented May 11, 2022

Hi @wu-hui, I considered the issues to be the same or have the same root cause. My observation is that getDocs never resolves when passed a query constraint - in my case, it doesn't resolve with a simple where constraint

const activeUsers = await getDocs(query(colRef, where('status', '==', 'active')))
console.log('resolved!') // <-- never reached

For some reason this doesnt get data for me. ^

But this does:
const q = query(colRef, where('status', '==', 'active'))
const activeUsers = await getDocs(q)

@firebase firebase locked and limited conversation to collaborators May 27, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

6 participants