Skip to content
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

Uncaught ReferenceError: Cannot access 'query' before initialization #79

Open
Molnfront opened this issue Nov 23, 2022 · 3 comments
Open

Comments

@Molnfront
Copy link

I just tried the example at: https://react-query-firebase.invertase.dev/firestore/querying-collections
and got this error above.

If I move the query before the function it will work, but then I am not able to pass parameters to the querys where clause.

I am using React 16.14.0.

import React from "react";
import { useFirestoreQuery } from "@react-query-firebase/firestore";
import {
  query,
  collection,
  limit,
  QuerySnapshot,
  DocumentData,
} from "firebase/firestore";
import { firestore } from "../firebase";

function Products() {
  // Define a query reference using the Firebase SDK
  const ref = query(collection(firestore, "products"));

  // Provide the query to the hook
  const query = useFirestoreQuery(["products"], ref);

  if (query.isLoading) {
    return <div>Loading...</div>;
  }

  const snapshot = query.data;

  return snapshot.docs.map((docSnapshot) => {
    const data = docSnapshot.data();

    return <div key={docSnapshot.id}>{data.name}</div>;
  });
}
@Molnfront
Copy link
Author

Ok, dumping this npm package. I build my own useHooks

@flexbox
Copy link

flexbox commented Mar 30, 2023

hey @Molnfront

If your problem is solved can you please close the issue?
It sparks joy for maintainers ✨

@decanTyme
Copy link

That's because there's two query identifiers declared there, the one imported and the const variable where you stored the result of useFirestoreQuery. It will try to use the const query = ... one instead of the import, so doing query(...) wouldn't work. You also get the error because it tried to use the variable before it was declared (and initialized). Renaming one of them should fix it. I think that's an oversight on the docs tho.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants