1

I am trying to cache items that get pulled from a Firebase database, I'm using React Native.

Getting and posting data works fine, the problem happens when trying to cache the data.

When internet gets turned off it shows the error:

@firebase/firestore: Firestore (8.2.2): Connection webchannel transport errored: [object, Object]'

when trying to cache.

For caching I use the "@react-native-async-storage/async-storage": "~1.15.0", library.

In the code below I get data and store it. When the internet is turned off the product stays visible for a few seconds then it throws the error and disappears.

The intended behaviour for this code is that the item stays on screen even when internet is off.

How do I fix this problem? Or is there a better way to sort out caching?

  async function getSelfMadeItems() {
    let list = [];
    if (getIsConnected === true) {
      let snapshot = await firestore
        .collection("SelfMadeProducts")
        .where("UserUID", "==", user.uid)
        .orderBy("MadeOn", "asc")
        .limit(5)
        .get()
        .then(async (querySnapshot) => {
          querySnapshot.forEach((doc) => {
            list.push({
              MadeOn: doc.data().MadeOn.toDate().toDateString(),
              //ImageUri: doc.data().ImageUri,
              id: doc.id,
            });
          });
          try {
            const jsonValue = JSON.stringify(list);
            if (!querySnapshot.empty) {
              await AsyncStorage.setItem("@SelfMade", jsonValue);
            }
          } catch (e) {
            console.log(e);
          }
          setproductsState(list);
        });
    } else {
      try {
        const ItemValue = await AsyncStorage.getItem("@SelfMade");
        if (ItemValue !== null) {
          const jsonValue = await AsyncStorage.getItem("@SelfMade");
          console.log("Json", JSON.parse(jsonValue));
        }
        jsonValue != null ? setproductsState(JSON.parse(jsonValue)) : null;
      } catch (e) {
        console.log(e);
      }
    }```
Tim Rooman
  • 11
  • 3
  • You might be using the wrong package. Try [react-native-firebase](https://rnfirebase.io/). Using the firebase package designed for web and native apps will result in errors like this. – Maximilian Dietel Dec 14 '21 at 15:37
  • @Sumsidum I use "@react-native-firebase/app": "^12.9.3", and "@react-native-firebase/firestore": "^12.9.3", do you think the problem lies there? – Tim Rooman Dec 14 '21 at 15:44
  • No that are the right packages. Sry but then I cant help you – Maximilian Dietel Dec 14 '21 at 15:49
  • Did you have a look at the answers from this [SO](https://stackoverflow.com/questions/65594133/react-native-to-firestore-firestore-8-2-1-connection-webchannel-transport-er) thread? – Sergi Dec 28 '21 at 10:36
  • @Sergi I took a good long look at it and tested what they say but none of it seems to work for me. – Tim Rooman Jan 07 '22 at 08:49

0 Answers0