Skip to content

firestore Failed to get document because the client is offline (even with persistance enabled) #3207

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
ulver2812 opened this issue Jun 11, 2020 · 5 comments

Comments

@ulver2812
Copy link

[REQUIRED] Describe your environment

  • Operating System version: Windows 10
  • Browser version: Chrome Version 83.0.4103.97 (Ionic 3)
  • Firebase SDK version: 6.6.2
  • Firebase Product: Firestore

[REQUIRED] Describe the problem

I'm using Firebase JS SDK with Ionic 3 and I enabled the offline capabilities with:

const firebaseConfig = {
      apiKey: "XXX",
      authDomain: "XXX",
      ...
    };
firebase.initializeApp(firebaseConfig);
firebase.firestore.enablePersistence();

but when I trying to retrive any document in firestore while I'm offline I get this error:
Failed to get document because the client is offline

I'm expecting to receive the cached data while I'm offline and not an error, or if there isn't any data an empty object or something like this.

Is this normal behaviour ?

For instance I get this error during this query (but I get the same error on any get query):

firebase.firestore().collection('users').doc(userUid).collection('activities')
      .where('date', '==', today.toDate()).get().then((querySnapshot) => {
      let dailyActivities = [];
      querySnapshot.forEach((doc) => {
        dailyActivities.push(new Activity(doc.data()));
      });
      this.dailyActivities$.next(dailyActivities);
    }).catch((error) => {
      console.error(error);
    });
@schmidt-sebastian
Copy link
Contributor

schmidt-sebastian commented Jun 11, 2020

@ulver2812 Thanks for writing this up.

The SDK should raise snapshots from cache if there is cached data available. We will not raise a snapshot from cache if there are no documents that match your query or get()s. Can you confirm that you have populated the cache and have run the same query before you went offline?

@ulver2812
Copy link
Author

@schmidt-sebastian thanks for your reply, I made some test and I discovered that if I follow these steps it works:

  • request some data when online
  • go offline
  • request the same data and get the same response like you are online

So in this way it works but if I don't have cached data I get an error? Is this the normal behaviour?

So I need to handle this scenario in the catch block in some way if the data are not cached?

Thanks

@PandaPalumbo
Copy link

PandaPalumbo commented Jun 14, 2020

This just started happening to me yesterday. My code worked. I get back from running errands and started getting this error. But without enabling persistence..

Code:


firebase.initializeApp(config);
firebase.auth().setPersistence(
  firebase.auth.Auth.Persistence.LOCAL
);
firebase.firestore.setLogLevel("debug")

//this part is in a function in actual code
db.collection('users').doc(email).get()
      .then( (data) => {
        if(data.data()){
            if(data){
                console.log('User Exists...');
                callback(data.data());
            }
            else {
                console.log("Error: No such user exists!")
            }
        }
    }).catch((error)=>{
        console.error(error);
    });

Then in the @firebase/firestore debug in my console I get a bunch of data from the document I called, then I get these logs that point towards error in their context to me.

[2020-06-14T18:23:04.453Z]  @firebase/firestore: Firestore (7.15.0): Connection WebChannel received: {"targetChange":{"targetChangeType":"REMOVE","targetIds":[2]}}
[2020-06-14T18:23:04.465Z]  @firebase/firestore: Firestore (7.15.0): WatchChangeAggregator Detected inactive target 2
[2020-06-14T18:23:04.469Z]  @firebase/firestore: Firestore (7.15.0): WatchChangeAggregator Detected inactive target 2
[2020-06-14T18:23:04.470Z]  @firebase/firestore: Firestore (7.15.0): WatchChangeAggregator Detected inactive target 2
[2020-06-14T18:23:04.470Z]  @firebase/firestore: Firestore (7.15.0): MemoryPersistence Starting transaction: Get last remote snapshot version
[2020-06-14T18:23:04.473Z]  @firebase/firestore: Firestore (7.15.0): MemoryPersistence Starting transaction: Apply remote event
[2020-06-14T18:23:04.475Z]  @firebase/firestore: Firestore (7.15.0): MemoryPersistence Starting transaction: notifyLocalViewChanges

Then I get these console errors:

[2020-06-14T18:22:55.676Z]  @firebase/firestore:, Firestore (7.15.0): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds.
This typically indicates that your device does not have a healthy Internet connection at the moment. The client will operate in offline mode until it is able to successfully connect to the backend.
- node_modules\expo\build\logs\LogSerialization.js:166:14 in _captureConsoleStackTrace
- node_modules\expo\build\logs\LogSerialization.js:41:24 in serializeLogDataAsync
- ... 9 more stack frames from framework internals

Failed to get document because the client is offline.
- node_modules\expo-app-auth\build\AppAuth.js:53:7 in revokeAsync
- node_modules\@firebase\firestore\dist\index.cjs.js:11723:52 in <anonymous>
- node_modules\@firebase\firestore\dist\index.cjs.js:11659:9 in <anonymous>
* http://192.168.1.77:19001/node_modules%5Cexpo%5CAppEntry.bundle?platform=android&dev=true&minify=false&hot=false:205277:20 in <unknown>
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:250:17 in _allocateCallback$argument_0
- node_modules\react-native\Libraries\Core\Timers\JSTimers.js:146:6 in _callTimer
- ... 5 more stack frames from framework internals

I'm online, on my phone and on my PC right next to my router. For what its worth, I am using Expo React native, but It seems to use the latest javascript sdk.

package.json

    "firebase": "^7.15.1",
    "firebase-app": "0.0.1",
    "firebase-auth": "^0.1.2",

@rphlmr
Copy link

rphlmr commented Jun 14, 2020

@schmidt-sebastian thanks for your reply, I made some test and I discovered that if I follow these steps it works:

  • request some data when online
  • go offline
  • request the same data and get the same response like you are online

So in this way it works but if I don't have cached data I get an error? Is this the normal behaviour?

So I need to handle this scenario in the catch block in some way if the data are not cached?

Thanks

Hello,

Yes it's the normal behaviour to get an error with get on a collection if the service could not be reached (when online and nothing found you got a snapshot.exists set to false).
https://firebase.google.com/docs/reference/js/firebase.firestore.DocumentReference#get

Your steps are good. Fetch online data > automatic persist > get same response if offline

@schmidt-sebastian
Copy link
Contributor

@ulver2812 Yes, if there is no cached data you will get an error when the client is offline.

@PandaPalumbo Please make sure to use Firestore 7.15.1 (your log lines still say 7.15.0). This version includes an important fix to #2923, which could explain the behavior you are seeing.

Since this is working as intended, I am going to close this issue.

@firebase firebase locked and limited conversation to collaborators Jul 16, 2020
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

5 participants