Home > database >  react firebase hooks - cant use limit/where methods
react firebase hooks - cant use limit/where methods

Time:01-27

Im working with react hooks for firebase/firestore and React Native (expo). All working good to the moment when start using limit or where - operators. I'm getting an error that for ex. limit is not a function or just undefined. Same situation is for where

My code

import app from "../api/firebase";

...

const collectionRef = collection(getFirestore(app), 'clients')
const queryRef = query(collectionRef).limit(5)

const [clients = [], loading, error] = useCollectionData(
  queryRef,
  {
    snapshotListenOptions: { includeMetadataChanges: true },
  }
);

Error

TypeError: (0, _firestore2.query)(collectionRef).limit is not a function. (In '(0, _firestore2.query)(collectionRef).limit(5)', '(0, _firestore2.query)(collectionRef).limit' is undefined)

Deps:

 "@firebase/firestore": "^3.8.1",
 "expo": "~47.0.12",
 "expo-firebase-core": "~6.0.0",
 "firebase": "^9.16.0",
 "react": "18.1.0",
 "react-firebase-hooks": "^5.1.1",
 "react-native": "0.70.5"

CodePudding user response:

Posted answer with example query so if anyone facing the similar issue will find it easier as in the comments it is already resolved.

react-firebase-hooks is build using regular firebase SDK V9 so from firebase documentation for query limiting you should use query in the following way:

const collectionRef = collection(getFirestore(app), 'clients')
const queryRef = query(collectionRef, limit(5)); // ⇐

const [clients = [], loading, error] = useCollectionData(
  queryRef,
  {
    snapshotListenOptions: { includeMetadataChanges: true },
  }
);
  • Related