Dev environment for Google Cloud Functions - firebase

I followed the instructions and got the hello world route working online, but I cannot see how to get to a place where I can develop, i.e. to iterate locally. This is my code so far
exports.helloWorld = functions.onRequest((request, response) => {
response.send("Hello from Firebase!");
});
In particular I cannot access routes locally, and there is no reloading of code on save:
I tried firebase serve and then trying to reach localhost:5000/helloWorld but nothing happened at all.
firebase functions:shell led to an error being returned on the route:
"/helloWorld is not a recognized path."
I can see that from the firebase > I can run functions, but I'm trying to work with graphql so I wanted to use the graphiql web interface
And neither approach seemed to reload code when I changed things. ctrl+c, up-arrow, enter is currently my dev environment.
I'm surely missing stuff but https://firebase.google.com/docs/functions/local-emulator is difficult to follow. What am I missing?

If you've done everything correctly, firebase serve will give you the url of the HTTP function you're trying to emulate. It will contain the name of your project, the name of the Cloud region, and the name of the function. Something like this:
http://localhost:5000/YOUR-PROJECT/YOUR-REGION/helloWorld
If you don't see this URL in the output of firebase serve, then no functions are being emulated. Why that's happening is impossible to say with what you've provided here. Perhaps you're not modifying the correct index.js. Or you just forgot to save the file. Or perhaps you're running firebase serve from the wrong project location. In any event, if you're still having problems, you'll need to provide the exact steps you're taking from the moment of project creation that reproduces the problem.

Related

Changing firestore-debug.log location

I have a watcher in my Firebase project that automatically compiles and restarts the emulator whenever I change my code. It's nice except I think because of some race conditions I get a new firestore-debug.log or asidfhusudf.log file every time I save my code. How can I disable the log files or at least change the location where they are made?
firestore-debug.log is present in the same folder as firebase.json. You can see it in the directory you ran the command which will have a log of all the requests.
You are using typescript for functions so you can refer to the documentation on how function logs works for Typescript projects :
During firebase deploy, your project's index.ts is transpiled to index.js, meaning that the Cloud Functions log will output line numbers from the index.js file and not the code you wrote. To make it easier for you to find the corresponding paths and line numbers in index.ts, firebase deploy creates
functions/lib/index.js.map. You can use this source map in your preferred IDE or via a node module.
How debug output is stored in the firestore-debug.log is mentioned in the Documentation :
A basic debug function that prints Security Rules language objects, variables and statement results as they are being evaluated by the Security Rules engine.The outputs of debug are written to firestore-debug.log.
The debug function can only be called inside Rules conditions.debug function blocks are only executed by the Security Rules engine in the Firestore emulator, part of the Firebase Emulator Suite. The debug function has no effect in production.Debug logfile entries are prepended by a string identifying the Rules language data type of the log output (for example, string_value, map_value).Calls to debug can be nested.Currently, the debug feature does not support the concept of logging levels (for example, INFO, WARN, ERROR).
You can also refer to the stackoverflow case where Kiana and Daniel have provided a brief explanation on how logs older than 1 month got disabled automatically.
You can see it directly within the Firebase console. When trying to choose the timestamp for the logs, logs older than 1 month are disabled, which means are
automatically deleted.
You can refer to the documentation for getting an understanding of how Query language is used to filter logs in the Emulator suite UI.
Currently, there's no way to change the location since it's hard coded: https://github.com/firebase/firebase-tools/blob/c0f7acc80e8c4c2d174f97c5c41d6114e192fd23/src/emulator/downloadableEmulators.ts#L205

Firebase Deploy causing multiple errors and not deploying

Hello I am following this tutorial in order to do push notifications to multiple devices at once in Expo React Native using Firebase's cloud functions. The push tokens are stored in my RealTime-Database, but when I attempt to integrate this step after running firebase login and firebase init, I updated my index.js file to contain this code. After saving this file, I attempt to do firebase deploy and I get 70 errors. I tried to deploy it from my app directory, and didnt work, and I tried to do it from my functions directory and that didn't work. I deleted my node_modules and package-lock, and I tried to rerun expo again and clearing my npm cache. Nothing worked. I already had my firebase app initialized somewhere else, could that be the reason why? But my App itself still works, so I am not getting an error saying that the Firebase app is being initialized twice. I really dont know what else do to. Is the ESLint causing all of these errors? Should I run firebase init again without adding ESLint? Please let me know. My errors are below.
When deploying your code, by default, your code is linted using a tool called ESLint.
The "errors" you see are eslint complaining about trivial things like missing semicolons at the end of a line, using ' instead of ", improperly indented code, among other things. If you ever don't understand what a rule is trying to do, you can visit https://eslint.org/docs/rules/{rule-name} (such as https://eslint.org/docs/rules/semi).
As shown in the error message, you can use eslint . --fix to automatically fix most of these. Just enter into your functions directory and execute eslint . --fix.
Most IDEs have a keyboard shortcut for automatically formatting your code to your rules. In VSCode for example, this shortcut is Alt+Shift+F.

Firebase Hosting and Github Action error "auth/invalid-api-key"

I'm using React for my project along with Firebase. Using Github Actions my code is successfully deployed to firebase.
But when I visit the hosted URL It gives me an error in the console saying
"Your API key is invalid, please check you have copied it correctly."
However when I deploy from the local system directly using firebase deploy It works fine.
I have taken the required care of my environment variables. But nothing seems to work.
It seems there is some problem while GitHub actions deploy the changes to firebase. Though all the build pass it gives me an error as stated above. Please Help.
This happen because GitHub actions don't have access to your .env on your local machine, you need to configure that on "GitHub Secrets" and put on your yml file.

Firebase Storage Trigger Not Working

I created a custom bucket which I use to serve content at a specific url. In Firebase I created a bucket called "images.mydomain.com" and I wanted to use this so my users can simply go to http://images.mydomain.com/imagename.jpg to access uploaded images.
I wanted to have a firebase trigger on my new bucket that does Google vision analysis on the image. Here is my function:
export const imageUploaded = functions.storage
.bucket('images.mydomain.com').object().onChange( event => {
//Cool stuff happens here
});
However, when I try to deploy the function I get this error in the console
⚠ functions[imageUploaded]: Deployment error.
Failed to configure trigger GCS Bucket: imagebucket
When I go to the function logs in Firebase I see the exact same error
Failed to configure trigger GCS Bucket: imagebucket
I also went to the Google Cloud Console to look at the functions to hopefully get some more insight on the issue. When I looked at the function, it had the correct bucket configured for the function but it had that exact same error without any explanation.
I have tried to create a new bucket using that but I get the exact same error. It seems that no matter what I do I keep getting the same error. I've tried adding permission and every It doesn't make any sense.
Does anyone know what this is about and/or how I can get this working? I had this working on the main bucket, but I really need this to work for my new bucket.
Thanks
A quick update:
I tried to deploy to my production environment (my app isn't in market yet so no one is actually using it) and it worked perfectly, the trouble is, I really need my development environment working which isn't right now.
To solve my issue I used this GitHub issue:
https://github.com/firebase/functions-samples/issues/66
Basically, there was no answer, I just had to shutdown my development project and start a new development project. Once I started a new project my functions deployed perfectly.

How do you un-deploy Cloud Functions for Firebase?

I was experimenting with Cloud Functions for Firebase and deployed one of the samples in the functions-samples repo and linked it to an existing project using firebase use --add.
Since I was just playing around, these files were not added to my version control, and I no longer have access to the machine this was done on. I would like to build on this function or create new ones, but I am unsure how to either 1) modify and re-deploy over the original function or 2) un-use --add and start fresh. I see the function in my firebase console but no 'delete' or 'remove' button and have read through the CLI docs looking for clues to no avail.
Thanks for adding functions to the firebase 'stack.' Any guidance is much appreciated.
Functions get deleted when they aren't present during a firebase deploy. Most commonly that'd be because because you removed the function from your index.js, but if you've removed the whole functions directory that'll work too.
To continue work on a function for which you don't have the source anymore, the easiest is to start fresh. The function(s) you deploy will replace the ones you deployed previously.
Alternatively, if you're partial to using the Google Cloud Console instead of the Firebase Console, the Cloud Console will show you the code for the currently-deployed function, so you can copy-paste it onto your local machine.
The Cloud Console also has a 'delete' button for every function, and even a web editor. Be aware when editing functions from the Cloud Console though: your next firebase deploy will overwrite any changes.
To delete a function explicitly use the following command:
$ firebase functions:delete myFunction
Replace myFunction with your function name. For detailed info. follow this link: https://firebase.google.com/docs/functions/manage-functions
Go to Google Cloud Console Cloud Functions and select project, then select the function you want to undeploy or delete. Select it and click delete.
To delete/undeploy a single function, you can delete the code for your function and then run the following in command line:
firebase deploy --only functions:YourFunctionName
Replace YourFunctionName with the name of your function
In the case that you are working on other functions that you are not ready to deploy or do not want to deploy all of your functions for any reason, then the code above can be handy. This also feels a bit safer since you're not redeploying everything :)
Now you can simply delete a function from the function dashboard as follows. Click on the three dots mark of relevant function in the right side corner.

Resources