Flutter FirebaseFirestore [cloud_firestore/failed-precondition] Issue - firebase

I'm trying to execute a query that has where and orderBy in a FirebaseFirestore.instance.collection('collectionName').where('fieldName1', isEqualTo: 'value1').orderBy('fieldName1', descending:true).snapshots() statement. I've also created a composite index for both fields defined in the above statement.
Though, I'm getting this error:
[log] [cloud_firestore/failed-precondition] Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console.
I'm using
firebase_core: ^1.3.0
cloud_firestore: ^2.3.0

I guess something might be wrong with the index. I recommend you to follow these steps:
Delete the index.
Run the function again. An error will be thrown.
Check the logs with adb logcat.
Some information will appear to create the appropriate index.

Related

List of Firestore Error Codes for Flutter

When using Cloud Firestore in my Flutter application I want to use the error codes to return a localized error message to the user. However, I did not find a list of all the possible error codes that could occur. Is there a list like that or do I have to handle the errors differently?
You can see the list of possible exception codes at the gRPC or at the documentation. But, you need to change the code from uppercase to all lowercase and replace underscore with dash. For example:
PERMISSION_DENIED -> permission-denied
NOT_FOUND -> not-found
And so on.
Also, you need to catch FirebaseException instead of PlatformException in the new version of cloud_firestore.
Please have a look at the exception codes from the firebase firestore documentation.

How to properly call FirebaseFirestore.instance.clearPersistence() in Flutter?

I'm facing the same problem as this guy question
But his accepted answer didn't helped me.
The problem:
When an user signs out, and another different user signs in, all data shown on my app is from the previous signed out user due to firebase caching system. I searched about this issue and found a solution that consists in calling this method:
FirebaseFirestore.instance.clearPersistence();
But everytime and everywhere I place this line of code, throws an exception saying I cannot call this method when the client is running:
Exception has occurred.
PlatformException (PlatformException(failed-precondition, Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console., {code: failed-precondition, message: Operation was rejected because the system is not in a state required for the operation's execution. If performing a query, ensure it has been indexed via the Firebase console., nativeErrorMessage: Persistence cannot be cleared while the client is running., nativeErrorCode: 9}))
so, how to call this method? or better, is there a best way to solve this problem?
It seems it's necessary to terminate the FirebaseFirestore.instance first.
At the end of my log off method I call:
await FirebaseFirestore.instance.terminate();
await FirebaseFirestore.instance.clearPersistence();
I get no errors thrown and everything seems to be working as it should now.
You should call it immediately after you initialize Firebase, and before you make the first query.
add this to your Login Button 'onPressed':
FirebaseFirestore.instance.terminate();
FirebaseFirestore.instance
.clearPersistence()
.then((value) => signinUser(email, password, context));

Permission errors unit-testing Google Cloud Functions calling firestore db

I'm following the guide at https://firebase.google.com/docs/functions/unit-testing, but I always get " FirebaseError: Missing or insufficient permissions" when I run my test. I have the correct service-account.json file and I'm setting up firebase-functions-test with it (that part goes without error).
I've tried it with the emulator running (setting the FIRESTORE_EMULATOR_HOST var), and without it (which I assume uses live data) but I always get this error, as soon as my code under test does a db request such as:
data = await db().collection('/machines').get()
So my function under test is getting called, but it just can't do anything.
I also tried calling firebase.initializeApp() the same way I do in my front-end app, but that doesn't help, although without it I get the error telling me to call Firebase App.initializeApp().
firebase use is set to my current project.
I also tried running my test as firebase emulators:exec jest, no difference.
Any ideas?

Why do I get "Client is not yet ready to issue requests" - Firestore get() fails when called by Schedule function

In my Firebase project I have a functions.pubsub.schedule().onRun() that runs every 5 minutes to perform some calendar related tasks. It needs to look up in my Firestore collections and does so with a .get() query.
This has been working fine until sometime 2020-03-13 in the morning where the function started to throw
2020-03-13 09:36:02.326 CET scheduledHooks 1042277797598294
Error: INTERNAL ERROR: Client is not yet ready to issue requests.
at Firestore.get projectId [as projectId] (/srv/functions/node_modules/#google-cloud/firestore/build/src/index.js:401:19)
at Query.toProto (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1556:42) at Query._get (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1466:30)
at Query.get (/srv/functions/node_modules/#google-cloud/firestore/build/src/reference.js:1457:21)
at FirebaseActivitiesCollection.<anonymous> (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:32:40)
at Generator.next (<anonymous>) at /srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:8:71
at new Promise (<anonymous>) at __awaiter (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:4:12)
at FirebaseActivitiesCollection.getActivitiesByInterval (/srv/functions/lib/collections/activities/FirebaseActivitiesCollection.js:27:16)
I can't track that I have changed anything, could it be that Firebase made some changes that I should be aware of or am I missing an obvious clue in this error message?
My other Cloud Functions and the Firebase JavaScript SDK still work fine.
Extra info: The same code works on my other environment where I have not deployed. This of course led me to search for changes in the deployed code, but I can't find any!?
It seems by the error, that the project was not connected and initialized correctly. As per the official API here, this error occurs when:
Returns the Project ID for this Firestore instance. Validates that
initializeIfNeeded() was called before.
I would recommend you to check that. In case you are still facing, I would say for you to contact the Firebase Free Support. Since you mentioned that it was working until yesterday morning, it might be some change on their side.
Hope this helps!
This turned out to be a very simple matter of initialization of Firebase:
Before I had:
// Initialize firebase
admin.initializeApp(functions.config().firebase)
..And it worked fine until it didn't anymore.
Now I have:
// Initialize firebase
admin.initializeApp()
... And it works again
Got the same error while running some jest tests involving firebase. Problem was that I didn't have GOOGLE_APPLICATION_CREDENTIALS environment variable set.
Since I was running them locally on my machine, exporting this variable in terminal before running the tests was sufficient for my case:
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/credentials/file.json

Cloud Functions for Firebase sometimes have Invalid credential error

Not appear every time, but sometimes this error appears in the log:
FIREBASE WARNING: {"code":"app/invalid-credential","message":"Credential implementation provided to initializeApp() via the \"credential\" property failed to fetch a valid Google OAuth2 access token with the following error: \"read ECONNRESET\"."}
Is there any way to handle this kind of error like retry?
Or is it okay to ignore it?
Just ensure that your machine time auto sync. and not manualy,
And your XXX......json from firebase is the latest downloaded. (if you dont sure, download it again - and this file will be the newer one)
that what helped me.
I am also facing this issue. Looks like it retries and get the job done but cloud functions are taking time to process data.

Resources