debugging firebase on local windows - firebase

I'm trying to debug firebase function on my local windows
as described in https://medium.com/#mwebler/debugging-firebase-functions-with-vs-code-3afab528bb36
I do
set FIREBASE_CONFIG={ databaseURL: 'https://invoice-manager-251609.firebaseio.com', storageBucket: 'invoice-manager-251609.appspot.com', projectId: 'invoice-manager-251609'}
functions start
and get
┌────────┬────────┬─────────┬─────────────────────────────────────────────────────────────────┐
│ Status │ Name │ Trigger │ Resource │
├────────┼────────┼─────────┼─────────────────────────────────────────────────────────────────┤
│ FAILED │ upload │ HTTP │ http://localhost:8010/invoice-manager-251609/us-central1/upload │
├────────┼────────┼─────────┼─────────────────────────────────────────────────────────────────┤
│ FAILED │ tst │ HTTP │ http://localhost:8010/invoice-manager-251609/us-central1/tst │
├────────┼────────┼─────────┼─────────────────────────────────────────────────────────────────┤
│ FAILED │ tst1 │ HTTP │ http://localhost:8010/invoice-manager-251609/us-central1/tst1 │
└────────┴────────┴─────────┴─────────────────────────────────────────────────────────────────┘
if I do
firebase emulators:start
I get
undefinedWarning, estimating Firebase Config based on GCLOUD_PROJECT.
Initializing firebase-admin may fail[2019-10-06T11:44:51.932Z]
#firebase/database: FIREBASE FATAL ERROR: Cannot parse Firebase url.
Please use https://.firebaseio.com
I also tried:
functions debug tst1
and get:
ERROR: Function worker crashed with exit code: 9
undefined(node:21096) [DEP0062] DeprecationWarning: `node --debug
and
node --debug-brkare invalid. Please usenode --inspectornode --inspect-brk` instead.
I tried with node-10 and node-8
I also tried this:
https://medium.com/#david_mccoy/build-and-debug-firebase-functions-in-vscode-73efb76166cf
and this
https://rominirani.com/google-cloud-functions-tutorial-debugging-local-functions-357c24829198
I get the same errors
whats going on?
how do I debugging firebase on local windows?

The guides in the question are correct, but
when running in Emulator, u have to set the db url manually
admin.initializeApp({
databaseURL: "https://<YOUR FIREBASE>.firebaseio.com"
});
here is the full cmd list:
functions start
functions deploy --trigger-http --timeout 600s funcName
functions inspect funcName
After that u can use chrome://inspect/ to connect to the process with the chrome debugger.
If you have issues, u can use:
functions logs read
NOTE: after functions start I still get the table with FAILED status - I ignore it

Related

I'm getting following Error while doing terraform apply

[root#terraform ~]# terraform plan -out terraform.tfstate
╷
│ Error: Error acquiring the state lock
│
│ Error message: 2 problems:
│
│ - Unsupported state file format: The state file could not be parsed as JSON:
│ syntax error at byte offset 1.
│ - Unsupported state file format: The state file does not have a "version"
│ attribute, which is required to identify the format version.
│
│ Terraform acquires a state lock to protect the state from being written
│ by multiple users at the same time. Please resolve the issue above and try
│ again. For most commands, you can disable locking with the "-lock=false"
│ flag, but this is not recommended.
#Ajay Kumar hi, problem is you are trying to pass .tfstate file as input and terraform is not able to parse same. terraform apply command expect .out file or .json file as input. Whereas .tfstate file is one where desired state of your infra is maintained (this is auto created by terraform itself). In this case what you can do is
Create one .tfvars file with all your configuration parameters (this will be served as input to terraform apply command)
touch input.tfvars
Next pass this input.tfvars file as input to your terraform apply command using -var-file flag and -out flag to generate .out file
terraform plan -var-file=input.tfvars -out inputplan.out
Next, you can use terraform apply command for above generated .out file
$ terraform apply "inputplan.out"
Refer this article for more information
https://medium.com/#hegdetapan2609/how-to-install-packages-on-multiple-similar-instances-at-same-time-using-terraform-182582af5260

Run Firebase Callable HTTP Cloud Function locally

I've a deployed a callable function, say foo, using
firebase deploy --only functions:foo
and I'm calling it in Flutter using:
var result = await FirebaseFunctions.instance.httpsCallable('foo')();
var data = result.data;
All good. But I'd like to run this function in the Firebase Emulator. When I run
firebase emulators:start --only functions
I can see the emulator up and running, and I'm connecting my Flutter app to the emulator using:
FirebaseFunctions.instance.useFunctionsEmulator('localhost', 4000);
However, I don't know how to invoke the callable foo in Flutter using the Firebase Emulator. I saw this related thread but it doesn't really answer my question.
EDIT:
Here's the console output:
✔ functions[us-central1-foo]: http function initialized (http://localhost:5001/my_project/us-central1/foo).
┌─────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! It is now safe to connect your app. │
│ i View Emulator UI at http://localhost:4000 │
└─────────────────────────────────────────────────────────────┘
┌───────────┬────────────────┬─────────────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├───────────┼────────────────┼─────────────────────────────────┤
│ Functions │ localhost:5001 │ http://localhost:4000/functions │
├───────────┼────────────────┼─────────────────────────────────┤
│ Firestore │ localhost:8080 │ http://localhost:4000/firestore │
└───────────┴────────────────┴─────────────────────────────────┘
Emulator Hub running at localhost:4400
Other reserved ports: 4500
I tried
useFunctionsEmulator('localhost', 4000);
useFunctionsEmulator('localhost', 5001);
but both of them throw the following error on calling httpsCallable('foo') but without the emulators, this function does work.
[ERROR:flutter/lib/ui/ui_dart_state.cc(209)] Unhandled Exception: [firebase_functions/unavailable] UNAVAILABLE
I suspect your host and port values might be incorrect.
When you start up your emulator, the logs will display all the ports that are being used for your various emulators. There's also the firebase.json file that has the port configurations for the emulators.
Also, localhost is not automatically identified by the clients. For Android you might have to configure networkSecurityConfig in your manifest by creating a network security config xml. For iOS, I think there should be something that needs to be added in the plist file - has to be checked confirmed.
Once your clients are able to access localhost (implicitly or explicitly), you can connect to your emulated functions. For example, I have explicitly state the host value in my code (this is not expected to change across restarts).
Here is the code that I use in Kotlin:
functions = FirebaseFunctions.getInstance()
/*
* Emulator
* */
if (emulatorMode) { //Local Testing
functions.useEmulator("10.0.2.2", 5001)
} else { //Live
functions = Firebase.functions
}
// Call the function and extract the operation from the result
return functions
.getHttpsCallable(function)
.call(inputData)
.continueWith { task ->...
It doesn't seem like a port or setup issue to me. Your console seems to show the expected output.
Try this:
Inside Flutter, where you want to call the function, add:
http.get(Uri.parse('http://localhost:5001/my_project/us-central1/foo'))
You're going to have to set an env variable, that asks your application to check what environment it is in. If dev, then it should use url above, if prod it can use the prod url
this._cloudFunctions = FirebaseFunctions.instance;
_cloudFunctions.useFunctionsEmulator(origin: "http://<System's IP>:5001");
Systems ip we can get by typing ipconfig in command prompt.
Make sure you connect your ip and run emulators start.
For Flutter
As per the Firebase Flutter Docs-
There is a useFunctionsEmulator module as part of your Firebase Functions instance.
You can initialize it as follows:
FirebaseFunctions.instance.useFunctionsEmulator('localhost', 5001);
For Web
If you cd into the functions directory you can run npm start which will, by default, build and run the emulators locally in the shell. I've found this to be the only solution for testing callable functions locally.
The firebase team has provided a connectFunctionsEmulator submodule in the V9 modular SDK which is designed specifically to interact with your app locally.
You can checkout the official docs for this Here.
Running npm run serve will serve the emulator that you are expecting but I have been running into issues testing callable functions via url.

Cannot trigger pubsub function in google cloud functions emulator

We are trying to create a development environment before publishing our firebase functions.
We have successfully installed and started a local emulator, can access our firestore database, can call the HTTPS endpoints and everything. Starting firebase emulators:start shows:
firebase emulators:start --import=./testdata
i emulators: Starting emulators: functions, firestore, hosting, pubsub
⚠ functions: The following emulators are not running, calls to these services from the Functions emulator will affect production: auth, database
⚠ Your requested "node" version "12" doesn't match your global version "14"
i firestore: Importing data from /home/forest/projects/icell/i_cell_parking_manager/firebase/testdata/firestore_export/firestore_export.overall_export_metadata
i firestore: Firestore Emulator logging to firestore-debug.log
i pubsub: Pub/Sub Emulator logging to pubsub-debug.log
i hosting: Serving hosting files from: webserver
✔ hosting: Local server: http://localhost:5000
i ui: Emulator UI logging to ui-debug.log
i functions: Watching "..../firebase/functions" for Cloud Functions...
✔ functions[createParkingReservationsForNextWeek]: pubsub function initialized.
✔ functions[reserveSpaceForUser]: http function initialized (http://localhost:5001/i-cell-parking-manager-backend/us-central1/reserveSpaceForUser).
┌────────────────────────────────────────────────────────────────┐
│ ✔ All emulators ready! View status and logs at localhost:4000 │
└────────────────────────────────────────────────────────────────┘
┌───────────┬────────────────┬──────────────────────────┐
│ Emulator │ Host:Port │ View in Emulator UI │
├───────────┼────────────────┼──────────────────────────┤
│ Functions │ localhost:5001 │ localhost:4000/functions │
├───────────┼────────────────┼──────────────────────────┤
│ Firestore │ localhost:8080 │ localhost:4000/firestore │
├───────────┼────────────────┼──────────────────────────┤
│ Hosting │ localhost:5000 │ n/a │
├───────────┼────────────────┼──────────────────────────┤
│ Pub/Sub │ localhost:8085 │ n/a │
└───────────┴────────────────┴──────────────────────────┘
Other reserved ports: 4400, 4500
We have a function called createParkingReservationsForNextWeek which is a timed single pubsub, we want to trigger it without needing to wait for the scheduler.
*/
exports.createParkingReservationsForNextWeek = functions.pubsub
.schedule(WEEKLY_NOTIFICATION_SCHEDULE)
.timeZone(TIMEZONE_EUROPE_BUDAPEST)
....
According to https://firebase.google.com/docs/functions/local-emulator#web we can do that by navigation to this URL in a browser: http://localhost:5001/i-cell-parking-manager-backend/us-central1/createParkingReservationsForNextWeek
But upon that we get this error (which can be seen in the emulator logs as well):
functions: TypeError: Cannot read property 'data' of undefined
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:580:28
at Generator.next (<anonymous>)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
at new Promise (<anonymous>)
at __awaiter (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:4:12)
at processBackground (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:577:12)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:673:23
at Generator.next (<anonymous>)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
at new Promise (<anonymous>)
⚠ Your function was killed because it raised an unhandled error.
In the logs (http://localhost:4000/logs):
14:50:31
I
function[createParkingReservationsForNextWeek]
Beginning execution of "createParkingReservationsForNextWeek"
14:50:31
W
function[createParkingReservationsForNextWeek]
TypeError: Cannot read property 'data' of undefined
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:580:28
at Generator.next (<anonymous>)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
at new Promise (<anonymous>)
at __awaiter (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:4:12)
at processBackground (/usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:577:12)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:673:23
at Generator.next (<anonymous>)
at /usr/local/lib/node_modules/firebase-tools/lib/emulator/functionsEmulatorRuntime.js:8:71
at new Promise (<anonymous>)
14:50:31
W
function[createParkingReservationsForNextWeek]
Your function was killed because it raised an unhandled error.
Cloud-firestore-emulator version is v1.11.9.
You can run a pub/sub function locally by calling it by name in the firebase shell:
Terminal 1: firebase emulators:start
Terminal 2: firebase functions:shell then createParkingReservationsForNextWeek()
If you need to include data on invocation (likely the cause of your error above), you can do so as well:
// invokes a function with the JSON message { hello: 'world' } and attributes { foo: 'bar' }
myPubsubFunction({data: new Buffer('{"hello":"world"}'), attributes: {foo: 'bar'}})
See the Firebase documentation for more details.

What is the local API endpoint, for a serverless REST API that uses the firebase emulator and node JS / Express?

I have a serverless REST API that uses NodeJS and Google Cloud Functions (Firebase). It works on production. But not locally for testing.
The app was created following the tutorial at: https://dev.to/levivm/creating-a-serverless-rest-api-using-google-cloud-functions-firebasefirestore-in-10-min-37km
When the firebase emulator is started locally the API endpoint seems not to work, and an error is returned:
URL http://my_local_server:8080/api/v1/my_api_route.
Output: Cannot GET /api/v1/my_api_route
The root url returns: http://my_local_server:8080/
{"status":"alive"}
Do you know what might cause the issue?
$ firebase emulators:start --only functions
i emulators: Starting emulators: functions
✔ functions: Using node#10 from host.
✔ functions: Emulator started at 0.0.0.0:8080
i functions: Watching "/home/ubuntu/environment/Crew-IQ/functions" for Cloud Functions...
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
✔ functions[webApi]: http function initialized (0.0.0.0:8080/crew-iq/us-central1/w...).
✔ All emulators started, it is now safe to connect.
For that application, the endpoint would be
http://localhost:8080/crew-iq/us-central1/webApi/api/v1/my_api_route
└───┬───┘ └─┬┘ └──┬──┘ └────┬────┘ └──┬─┘└─────────┬────────┘
host │ │ │ │ │
port │ │ │ │
project ID │ │ │
function region │ │
function name │
your express app

firebase deploy skips exported function

I recently added a new cloud function to my project, and for some reason when I deploy it gets skipped:
firebase deploy --only functions:stripeOperation
which results in
⚠ functions: the following filters were specified but do not match
any functions in the project: stripeOperation
The file structure is like this:
functions/
├── src/
│ ├── stripe
│ │ ├── index.ts
│ │ ├── myTrigger.ts
│ │ ├── anotherTrigger.ts
│ │ └── stripeOperation.ts
│ ├── index.ts
functions/src/stripe/index exports all 3 of the functions in the stripe folder.
functions/src/index exports them from the stripe folder:
export { stripeOperation, myTrigger, anotherTrigger } from './stripe';
Here's where it gets weird - myTrigger and anotherTrigger successfully deploy, but stripeOperation doesn't. There are no build errors. Firebase isn't giving me any clues as to why it's skipped. I checked and the transpiled code looks fine. stripeOperation is a callable function but the other 2 are firestore triggers. This is the signature of stripeOperation:
export const stripeOperation = functions.https.onCall((data, context) => {
...
});
Is there any way to determine why firebase won't deploy my function? I am using "firebase-functions": "^2.3.1"
Update: I have tried renaming the function, and completely switching out the function body with a previously deployed function, and neither worked.
You have to export callable functions separately from trigger functions. This doesn't make sense, so I filed an issue.
functions/src/index:
export { stripeOperation } from './stripe/stripeOperation';
export { myTrigger, anotherTrigger } from './stripe';

Resources