I want to run a firebase http cloud function locally accessing a real remote firestore database.
To do it, I'm running my functions locally using:
FIREBASE_CONFIG="/path-to-credential.json" GOOGLE_APPLICATION_CREDENTIALS="/path-to-credential.json" firebase emulators:start --only functions
As written here https://firebase.google.com/docs/admin/setup, this should permit the initialization of the admin sdk without any parameter. My functions index.js
is as following
import * as admin from 'firebase-admin';
import * as fromHttpFunc from './http/func';
admin.initializeApp();
export const httpFunc = fromHttpFunc.httpFunc;
When I then call the function in the browser, this is what I get in logs
[warn] ⚠ The Cloud Firestore emulator is not running so database operations will fail with a 'default credentials' error.
[debug] [2019-07-20T13:16:28.656Z] [runtime-status] Ephemeral server survived. {}
[info] > (node:41939) UnhandledPromiseRejectionWarning: Error: The incoming JSON object does not contain a client_email field
[info] > at JWT.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/jwtclient.js:165:19)
[info] > at GoogleAuth.fromJSON (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:294:16)
[info] > at GoogleAuth.getClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-auth-library/build/src/auth/googleauth.js:476:52)
[info] > at GrpcClient._getCredentials (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:107:40)
[info] > at GrpcClient.createStub (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/google-gax/build/src/grpc.js:223:34)
[info] > at new FirestoreClient (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/v1/firestore_client.js:128:39)
[info] > at ClientPool.Firestore._clientPool.pool_1.ClientPool [as clientFactory] (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/index.js:315:26)
[info] > at ClientPool.acquire (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/pool.js:61:35)
[info] > at ClientPool.run (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/pool.js:114:29)
[info] > at Firestore.readStream (/Users/michele/dev/clubup/backoffice/firebase/functions/node_modules/@google-cloud/firestore/build/src/index.js:995:26)
[info] > (node:41939) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 3)
[info] > (node:41939) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
[warn] ⚠ Your function timed out after ~60s. To configure this timeout, see
https://firebase.google.com/docs/functions/manage-functions#set_timeout_and_memory_allocation.
I don't get what's wrong, I also checked if the path to the credential is ok by doing cat /path-to-credential.json
and I have a json with the client_email
field.
This error is because of the failed quitting from firebase emulator. You already have the process of previous firebase emulator. For a new start, you have to find and stop the previous process.
Go to my local Firebase project path. Start the emulators using: firebase emulators:start. Create manually some mockup data using the GUI at http://localhost:4000/firestore using the buttons provided: + Start Collection and + Add Document. Export this data locally using: emulators:export ./mydirectory.
Launch the Local Emulator Suite with firebase emulators:start . The Cloud Functions and database emulators start up, automatically configured to interoperate. View the UI in your browser at http://localhost:4000 . Port 4000 is the default for the UI, but check terminal messages output by the Firebase CLI.
Use the @firebase/rules-unit-testing module to interact with the emulator that runs locally. If you get timeouts or ECONNREFUSED errors, double-check that the emulator is actually running. We strongly recommend using a recent version of Node. js so you can use async/await notation.
Managed to have the local cloud functions working with the real remote DB by making sure of the following:
run firebase login
and login into the project you want to use
follow this comment https://github.com/firebase/firebase-functions/issues/121#issuecomment-337065268 to make sure the file is not there (I had a invalid_grant
error)
run export GOOGLE_APPLICATION_CREDENTIALS="SERVICE_ACCOUNT_PATH.json"
in the terminal session you want to use (replace SERVICE_ACCOUNT_PATH)
serve your http functions NOT by using firebase emulators:start --only functions
but using firebase serve --only functions
. Currently, this is an undocumented behaviour: https://github.com/firebase/firebase-tools/issues/1412#issuecomment-504561828.
initialize the app as following (update YOUR_APP_ID):
admin.initializeApp({
credential: admin.credential.applicationDefault()
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With