I've been trying to implement some Cloud Functions that reads data from Cloud Firestore. I want to test these locally, however I cannot get the Cloud Firestore emulator to run. My assumptions are that it's where the problem is, but it might be somewhere else - if that's the case please let me know.
Here is my function:
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'
admin.initializeApp()
//Functions to fetch the documents in the user collection and send them to the client.
export const getUserDocs = functions.https.onRequest((request, response) => {
admin.firestore().doc('/Users/{documentId}').get()
.then(snapshot => {
const data = snapshot.data()
response.send(data)
})
.catch(error => {
response.status(500).send(error)
})
})
This is the error I get when I run the command firebase serve --only functions :
Carlo#Carlos-Air-2 functions % firebase serve --only functions
⚠ Your requested "node" version "8" doesn't match your global version "13"
✔ functions: functions emulator started at http://localhost:5000
i functions: Watching "/Users/Carlo/app/functions" for Cloud Functions...
✔ functions[getUserSports]: http function initialized (http://localhost:5000/app/us-central1/getUserSports).
i functions: Beginning execution of "getUserSports"
⚠ functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
⚠ External network resource requested!
- URL: "http://179.252.163.233/computeMetadata/v1/instance"
- Be careful, this may be a production service.
⚠ External network resource requested!
- URL: "http://metadata.google.internal./computeMetadata/v1/instance"
- Be careful, this may be a production service.
i functions: Finished "getUserSports" in ~9s
I've tried starting from scratch again and uninstalling and reinstalling but with no success.
I've also watched the tutorial series on YouTube but for some reason my code doesn't work.
Any help is greatly appreciated.
I was getting the following error when trying to run firebase for local testing:
The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
What I did wrong was no select both Firestore and Functions (see image).
then there are a number of commands available to spin up local testing:
firebase serve
firebase serve --only functions
firebase emulators:start --only=functions
firebase emulators:start
The last one (#4) resolved the issue for me.
There is no error in your output console.
It's just a warning saying that you are not running Firestore locally, which is totally normal since you run the command with --only functions and thus started only the cloud functions part (without firestore, rules, etc)
Related
Hi i am having firebase cloud functions, When i run firebase emulator in local they are not displaying in emulator.below is my functions code
const functions = require("firebase-functions");
const admin = require("firebase-admin");
const DomParser = require('dom-parser')
admin.initializeApp(functions.config().firebase);
exports.storeBooking = functions
.region("europe-west1")
.pubsub.topic("responses")
.onPublish((message) => {
/// some logic
});
when i run emulator must show function storeBooking, But in emulator it is showing as below
Watching "D:\backend-functions\booking" for Cloud Functions...
function ignored because the pubsub emulator does not exist or is not running.
function ignored because the pubsub emulator does not exist or is not running.
The emulator will show like this:
Watching "D:\backend-functions\booking" for Cloud Functions…
function ignored because the pubsub emulator does not exist or is not running.
function ignored because the pubsub emulator does not exist or is not running.
Which means function was found but for some reason firebase cannot connect to pubsub emulator, despite all the configs.
As per the documentation, you need to set up admin credentials to test your functions to interact with Google APIs or other Firebase APIs via the Firebase Admin SDK.
The cloud pubsub requires the setup steps described in this section. This applies whether you're using the Cloud Functions shell or firebase emulators:start.
You can follow the instructions To set up admin credentials for emulated functions from documentation.
For more information you can check with this Stackoverflow Link.
I have a Firebase function createUser that is triggered by auth.user().onCreate.
Whenever I serve functions locally, it is ignored.
I get the same output when I run firebase serve, firebase serve -only functions,firestore, firebase emulators:start, or firebase emulators:start --only functions,firestore:
⚠ functions: Your GOOGLE_APPLICATION_CREDENTIALS environment variable points to /home/david/Sync/alikely/key.json. Non-emulated services will access production using these credentials. Be careful!
✔ functions[createItem]: http function initialized (http://localhost:5000/alikely-ce1d5/us-central1/createItem).
i functions[createUser]: function ignored because the firebaseauth.googleapis.com emulator does not exist or is not running.
As shown above, I have attempted to follow the instructions to setup admin credentials here: https://firebase.google.com/docs/functions/local-emulator.
I have initialized functions and firestore with firebase init, and my project directory contains a firestore.rules, firestore.indexes.json, and a firebase.json. Are the contents of one of these files to blame?
How can I locally emulate all of my functions for full local development? How can I enable the "firebaseauth.googleapis.com emulator"?
The documentation you linked says:
The Firebase CLI includes a Cloud Functions emulator which can emulate
the following function types:
HTTPS functions
Callable functions
Cloud Firestore functions
Auth functions are currently not supported, just the three types mentioned above.
I'm starting my cloud function emulator according to the documentation here:
firebase emulators:start --only functions
Things look smooth for the onRequest functions:
i Starting emulators: ["functions"]
⚠ Your requested "node" version "8" doesn't match your global version "10"
✔ functions: Emulator started at http://localhost:5001
i functions: Watching "/Users/.../functions" for Cloud Functions...
i functions: HTTP trigger initialized at http://localhost:5001/sweever-prod/us-central1/sendMailTrigger
i functions: HTTP trigger initialized at http://localhost:5001/sweever-prod/us-central1/sitemapData
but falls flat when there are onTrigger functions:
Ignoring trigger "newUserSignedUp" because the Cloud Firestore emulator is not running.
Ignoring trigger "newItemAdded" because the Cloud Firestore emulator is not running.
Ignoring trigger "itemUpdated" because the Cloud Firestore emulator is not running.
What might the cause of onTrigger functions being ignored locally? I'm led to believe Firestore can handle these locally given the documentation's reference: "Cloud Functions emulator which can emulate the following function types: ... Cloud Firestore functions." I think the onTrigger would be a Cloud Firestore function but might be confusing the terminology.
If you want to run the emulators for multiple products (Hosting and Firestore), you should be starting the emulator with the command:
firebase emulators:start
Right now, you are just starting the emulator for functions that don't involve Firestore. Please refer to the documentation for more information.
I trying to test my google firebase cloud functions on my local computer using firebase emulator:start. However the local instance of the cloud function is not being executed, instead the instance on Google Cloud gets executed.
I followed these steps for setting the emulator up: https://firebase.google.com/docs/functions/local-emulator
$ export GOOGLE_APPLICATION_CREDENTIALS='path/tofile'
$ firebase setup:emulators:firestore
$ firebase init firestore
$ export FIRESTORE_EMULATOR_HOST=localhost:8080
$ firebase emulators:start
Starting emulators: ["functions","firestore","hosting"]
⚠ Your requested "node" version "8" doesn't match your global version "10"
✔ functions: Emulator started at http://localhost:5001
i firestore: Logging to firestore-debug.log
✔ firestore: Emulator started at http://localhost:8080
i firestore: For testing set FIRESTORE_EMULATOR_HOST=localhost:8080
✔ hosting: Emulator started at http://localhost:5000
i hosting: Serving hosting files from: www
✔ hosting: Local server: http://localhost:5000
i functions: Watching "/home/borch/Documents/Ionic/myproject/functions" for Cloud Functions...
i functions: Setting up Cloud Firestore trigger "myGCFunction"
✔ functions: Trigger "myGCFunction" has been acknowledged by the Cloud Firestore emulator.
As you can see the trigger is recognized by the cloud firestore emulator.
But the functions that gets executed every time I call myGCFunction is the one on Google Cloud. I think it should execute the function from my local API running at localhost:8080, correct?
I realize that only the function on the cloud is executed because I checked the firebase function console with new records of that function execution.
I updated firebase-admin and firebase-functions to its latest version:
$ cat functions/package.json | grep firebase-
"firebase-admin": "^8.2.0",
"firebase-functions": "^3.0.2",
Thanks.
The local emulator is not going to respond to changes in the cloud. You have to actually change the locally emulated database in order to trigger locally emulated functions.
(Imagine if you accidentally started a local emulator that responded to changes against a busy production database - that would not scale well on your local machine, would it? The local environment must be totally isolated from any cloud hosted environment in order to work sanely.)
I've just set up the Emulator per the instructions here:
Dan$ firebase emulators:start --only functions
Starting emulators: ["functions"]
✔ functions: Emulator started at http://localhost:5001
i functions: Watching "/Users/danfein/Desktop/reviews/backend/functions" for Cloud Functions...
I understand there's something running at :5001. My local DB environment is not running locally (as I understand that's not something supported by Firebase) so because my connections are going out to the web, I don't understand how to trigger something to :5001.
How can I, with my running React instance (running on :3000 if relevant), trigger one of the cloud functions currently living in my functions folder.
The functions do currently run and are appropriately triggered in the cloud.
You will want to set up your development environment as described in the documentation. There is a new emulator suite that ties together Cloud Firestore (which actually can be emulated) and Cloud Functions.