Firestore has already been started and cannot be changed - Nuxt 2, Firebase Emulator - firebase

EDIT (ANSWER): There were two issues with my code. One was that I was passing "http://localhost" instead of just "localhost" to the connectFirestoreEmulator line. The second was that I needed to check db._settingsFrozen before running the emulator line in order to prevent it from being double-initialized. I believe this has something to do with how Nuxt runs its code on the server side vs client side: I think Nuxt shares context/state across sessions on the server side, leading to the emulator line getting run more than once.
I have a Nuxt 2 app that I've connected to Firebase production successfully for a while now. I'm trying to set it up now to use the emulator instead of production when in local environment (indicated by an env variable), but I'm getting an error with the Firebase module specifically. There doesn't seem to be any issues using the auth emulator (which I've also set up). Code (plugins/firebase.js):
const app = initializeApp(firebaseConfig);
const auth = getAuth(app);
const db = getFirestore(app);
export const services = { auth, db };
export default (context) => {
if (context.env.appEnv === 'dev') {
console.log('dev', parseInt(context.env.firestorePort))
connectFirestoreEmulator(db, 'http://localhost', parseInt(context.env.firestorePort));
connectAuthEmulator(auth, `http://localhost:${context.env.authPort}`);
}
}
I have narrowed it down to the "connectFirestoreEmulator" line which when commented out, there are no errors. When it's included, this is the error I get:
Firestore has already been started and its settings can no longer be changed. You can only modify settings before calling any other methods on a Firestore object.
Finally, when I set the plugin to run on client-side only, it no longer errors out which is odd. I'll probably just keep working with this client-only for now, but I would like to be able to run the firebase stuff on the server too ideally.
Any ideas/guidance is appreciated. Thanks in advance!
I've Googled the relevant terms and found one other question that was similar to my question ("Firebase Error : Firestore has already been started and its settings can no longer be changed." connecting Firebase v9 with Firestore Emulator). However, because I don't have enough reputation on SO, I can't comment to ask the OP if he ever found out what was happening (which I would normally do before asking my own question, but I'm not being given a choice here).
I also even looked at the nuxt-firebase npm package's source code (https://github.com/nuxt-community/firebase-module) to see how they may have set up the emulator, but their plugin/code is written so differently from mine that it was unhelpful.

Related

(firebase functions) Error: Forbidden Your client does not have permission to get URL /

I have problem when invoking deployed function in firebase. I have an editor role in the firebase project and when I deployed functions, didn't have any problem with invoking them. When I deployed a new function yesterday, I got the error message that says
Error: Forbidden
Your client does not have permission to get URL / < Function Name > from this server.
Nothing has been changed to my role. It is strange that since yesterday, whatever function I deployed, threw those errors.
In gcp console/cloud functions, where you can see permissions of the function that was selected, I've noticed that "cloud functions invoker" was not assigned to that function. I thought this should be added to any function by default as long as I have an editor access but strangely it does not add them anymore. other functions that were deployed since yesterday have the same issue
any suggestions or advices will be appreciated. Thank you
Please Review Allowing unauthenticated function invocation
As of January 15, 2020, HTTP functions require authentication by default. You can specify whether a function allows unauthenticated invocation at or after deployment.
So here's the answer from the firebase team
The issue you are experiencing is likely caused by the fact that after January 15, 2020, Google Cloud Functions automatically creates HTTP functions to be >private by default.
Please, update the CLI, by running the following command:
npm install -g firebase-tools
This will ensure that future HTTP functions that are created will be publicly accessible.
Lastly, for the existing functions that has the permission issues, you will need >to manually set a function to public using Cloud Console or gcloud CLI.
If you have any questions or you are still facing this issue, please, don’t >hesitate to write back.
edited*
There could be several reasons to cause this issue.
check your function endpoint url make sure there's no typo or space
In the gcp console, make sure you have permission to invoke function https://console.cloud.google.com/functions/list?project=<YOUR_PROJECT_ID>
If the above two are checked, delete your function and redeploy your them again
Unfortunately, you can't do this in Firebase, you have to go into the Google Cloud project which 'hosts' your firebase project. You can follow this guide by Google, and have a look at the screenshots below:
You should see Allow unauthenticated now
To allow unauthenticated invocation of a function, you add the special allUsers member id to the function and grant it the Cloud Functions Invoker role:
You can limit domain access in your function, for example:
exports.myTest= async(req, res) => {
res.set('Access-Control-Allow-Origin', 'foo.com');
...etc
I defined my Firebase cloud functions in typescript/JS and deploy using Firebase CLI. I got this error after customizing the deployment settings, and I fixed it by specifying invoker="public" - I did not need to dig into IAM settings or use the console or CLI to fix.
export const serve = functions
.region("us-west2")
.runWith({
invoker: "public", // this is the magic line
})
.https.onRequest(
async (request: functions.Request, response: functions.Response) => {
// ...
})
updating firebase-tools wasn't enough in my case because i already deployed that function and updating it didn't fix the issue, i had to delete it and deploy again

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

FIREBASE WARNING: Invalid query string segment - Warning when deploying simple Firebase Cloud function

This morning I started noticing a large amount of "FIREBASE WARNING: Invalid query string segment" errors in my functions log. In an attempt to figure out what was going on I ended up making various changes to function and deploying all my cloud functions several times.
While doing this I noticed that I was getting the error on a function that:
Does not have a Firebase Query in it.
Has not been invoked in months (it's a test function).
This leads me to believe that a change was made Firebase Cloud Functions recently that may be sporadically generating this error on deploy and/or execution of cloud functions regardless of whether the function has anything to do with real time database queries. Has anybody else noticed this error or have any insight into why it is occurring?
Relevant part of my cloud index.js file is as follows (should be enough to replicate the issue):
const cors = require('cors')({origin: true});
const functions = require('firebase-functions');
const admin = require('firebase-admin');
const firebaseRef = admin.database().ref();
const firestoreRef = admin.firestore();
exports.testCloudFunctions = functions.https.onRequest((request, response) => {
cors(request, response, () => {
const params = request.query;
console.log(params)
response.status(200).send({success: true, message: "Test Successful", params: params});
});
});
After talking with Firebase support, I learned this bug was introduced in the npm dependency #firebase/database: 0.1.10. You will have this dependency if you also have the latest firebase-admin dependency.
The warning
shouldn't have any impact on the function's overall processing.
If you want to remove the warning, you can force the npm dependency of #firebase/database: 0.1.9. I tested this adjustment and the warning was removed from my Firebase Console Logs.
The support agent also stated the fix has been made, but not released, so I would expect #firebase/database: 0.1.11+ to not experience this bug.
Looks like a recently introduced bug which will hopefully be fixed soon.
We having the same problem today. Maybe some maintenance in the platform. Maybe some Google dev will soon say something about this.
It seems they are having issues with notification.
You can check for the status here: https://status.firebase.google.com/
I am new to Firebase today.
The code snippet I generated for the Firebase db connection had this firebase.js :
<script src="https://www.gstatic.com/firebasejs/4.10.0/firebase.js"></script>
This is generating the message : 'FIREBASE WARNING: Invalid query string segment:', but is also returning the data.
If I use a different version of firebase.js, either .../4.8.0/... or .../4.9.0/... , no error/warning message is generated.
I am running Google Chrome on Windows 10.
Update: It's officially a bug and you can ignore it until they release a fix in the next update or add version of #firebase/database: 0.1.9 to dependencies if you don't want to wait.
Seems like an internal error or change. Also getting this firebase warning and my code was working fine yesterday using '' syntax as well. However my function still works. Looks like they're doing some heavy dev work on it as its in beta and issues are coming up now and then.
Wait for fix or report here:
https://firebase.google.com/support/contact/bugs-features/
Status here (fixes usually take time to appear on here):
https://status.firebase.google.com/
Copy of 48830081 and possibly 48832566

Firebase Cloud Firestore throws "client is offline"

About 3 out of 4 times loading the page I get this error from the firestore client SDK (web). This slows development down tremendously. Auth, Realtime DB and Storage work perfectly fine.
Failed to get document because the client is offline.
Is there something I can do about this?
There's currently an interaction between the clients and routing on the backend that results in this problem. As a result the client believes the network is failing and goes offline.
We'll be rolling out a fix for this soon. Exact times I can't promise but this is among our top priority issues to fix.
Thanks for your patience.
I updated all my packages and I ran into this issue.
I enabled offline persistence for my web app and that fixed the problem:
firebase.firestore().enablePersistence()
https://firebase.google.com/docs/firestore/manage-data/enable-offline
For me, the same error happened when
project_id is wrong
port for emulator is 8081 instead of 8080. (when port was 8081 the emulator was working well, with UI opened normally and so on but the error kept showing. I fixed the problem by changing the port back to 8080)
The error message is very misleading.
I have just spent about 3 hours trying to fix it. There was something wrong with my emulator. I had to remove it, then created new and now it is working. So if you face this issue, give it try.
The problem might occur because you're integrating firebase 'web' SDK in your React Native (RN CLI not Expo) app. So in order to use firebase in your react native app you must follow this tutorial: https://rnfirebase.io/
It must work.
For me, just restarting the emulator fixed it.
In my app as well this error occurs at the first run after installation. I was able to get rid of above bug by refreshing/restarting the Activity.
if (e.getMessage().equals("Failed to get document because the client is offline.")){
finish();
startActivity(getIntent());
}
This code must be placed in addOnFailureListener.
In my case, I was using a leading slash in the database URL:
admin.initializeApp({
credential: admin.credential.cert(CREDENTIALS_PATH),
databaseURL: "https://databasename.firebaseio.com/"
// remove this ^
});
I had some problem. but I've just fixed it. check your projectId is correct. when you use cloud firestore, you need it.
export const environment = {
production: false,
firebaseConfig: {
apiKey: 'XYZ',
authDomain: 'XYZ.firebaseapp.com',
databaseURL: 'https://XYZ.firebaseio.com',
storageBucket: 'XYZ.appspot.com',
projectId: 'XYZ' // <--- make sure project ID is here
}
};
For me, enabling ({ synchronizeTabs: true } ) seems to have helped.
Just in case if someone ever faces this issue on the web (server side code), simply try restarting the server and again ping the endpoint that connects to firebase. This worked for me!
In my case I was using Chrome Extension Moesif CORS. I disabled the extension and it worked.
In my case, I was getting this issue along with a warning that I should change my database location as it was in a different region.
I added a databaseURL property in the firebaseconfig with the url of my dsatabase and it worked properly after that.
My problem was that I previously tested my app with the firebase-local-emulator and therefore had some configurations for it still enabled, for example:
Firebase.firestore.apply {
if (DomainConstants.isOnTest) useEmulator("10.0.2.2", 8080)
}
Because I forgot to set isOnTest = false, the app relied on the firebase-local-emulators data (which was not running) and threw client is offline error.
Long story short: Check if you configured your project using firebase-local-emulator and turn it off when you want to use your "real" database
Saw this error in the console, Enabling Datastore might help.
#firebase/firestore: Firestore (9.6.9): Could not reach Cloud
Firestore backend. Connection failed 1 times. Most recent error:
FirebaseError: [code=not-found]: The project my-project does not exist
or it does not contain an active Cloud Datastore or Cloud Firestore
database. Please visit http://console.cloud.google.com to create a
project or
https://console.cloud.google.com/datastore/setup?project=my-project to
add a Cloud Datastore or Cloud Firestore database. Note that Cloud
Datastore or Cloud Firestore always have an associated App Engine app
and this app must not be disabled. 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.
Try to use VPN, It will work!.
Me i switched to VPN and it worked 100%. Sometimes Firebase fail to respond to IPs addresses of some countries.

Google Cloud Functions with ECONNRESET errors until I redeploy

I'm using Google Cloud Functions to:
Watch for a new Firebase entry
Download a file that's referenced in the Firebase entry
Generate a thumbnail based on that file.
Upload the thumbnail to the cloud bucket.
Unfortunately I'm getting ECONNRESET errors repeatedly on step 4, and the only way to fix it seems to be to redeploy the function. Any ideas how to further debug this?
Edit:
It seems like many times when this happens, when I try to deploy the function again, it errors, and I have to run the deploy twice. Is something hanging or something?
Update May 9 2017
According to this thread, the google cloud nodejs API developers have made some changes to the defaults that are used when initializing that should solve these ECONNRESET socket issues.
From #stephen++ on github GoogleCloudPlatform/google-cloud-node issue 2254:
We have disabled the forever agent by default in Cloud Function
environments. If you un- and re-install #google-cloud/storage, you
will pick up the new behavior automatically. Thanks for all of the
helpful debugging, everyone!
Older Post Follows:
The solution for me to similar ECONNRESET issues using storage on the cloud functions platform was to use npm:promise-retry, but set up your own retry strategy because the default of 10 retries is too many.
I reported an ECONNRESET issue with cloud functions to Google Support (which you might star if you are also getting ECONNRESET in this context but not in other contexts) and they replied with a "won't fix" that the behavior is expected. Google support said the socket that the API client library uses to connect times out after a few minutes, and then when your cloud function tries to use it again you get ECONNRESET. They recommended adding autoRetry:true when initializing the storage API, but that did not help.
The ECONNRESETs happen on the read side too. In both read and write cases promise-retry helps, and most of the time with only 1 retry needed to reset the bad socket.
So I wrote npm:pipe-to-storage to return a promise to do the retries, check md5, etc., but I haven't tested it with binary data, only text, so I don't know if you could use it with image files. The calls would look like this:
const fs = require('fs');
const storage = require('#google-cloud/storage')();
const pipeToStorage = require('pipe-to-storage')(storage);
const source = ()=>(fs.createReadStream("/path/to/your/file/to/upload"));
pipeToStorage(source, bucketName, fileNameInBucket).then(//do next step);
See also How do I read the contents of a new cloud storage file of type .json from within a cloud function?
You can directly report a bug to the Firebase Support team, or open a support ticket with Firebase to troubleshoot a specific issue.
You may also report a Cloud Functions specific issue in the Google Issue Tracker, which is similar to Stack Overflow in that it is accessible by the public (but specifically used for filing issue reports).

Resources