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.
Related
I'm learning to set up Firebase Emulators correctly to work on my projects and I came up with a problem. I can setup the emulators and make them work locally, however, when trying to access firestore it seems to try to access the real Firestore Instance instead of the emulator.
Right now I'm initializing the app like this (in Cloud Functions)
admin.initializeApp();
const db = admin.firestore();
But when I'm running a function I'm getting:
Failed to initialize and load triggers. This shouldn't happen: Failed to read credentials from file GOOGLE_APPLICATION_CREDENTIALS.json: Error: ENOENT: no such file or directory, open 'GOOGLE_APPLICATION_CREDENTIALS.json'
The thing is that if I use the credentials I generated for my project it will work with the real Firestore instance instead of the emulator.
How should I make credentials for my emulated services?
If you are using Firebase Functions emulator as well then Admin SDK will connect to all the running emulators e.g. if only Auth emulator is running then it'll use the emulator and connect to production for other services like Firestore. You can explicitly set the FIRESTORE_EMULATOR_HOST environment variable and Admin SDKs will use the emulator then.
Checkout the documentation for more information.
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)
Today i ran into a problem with my Firebase Cloud Function that generates images sizes when new images are being uploaded to the Firebase Storage. This problem causes my quota to exceed after a couple uploads and when the quota exceeds the limit i am not able to use firebase anymore. Now i am trying to run this Cloud Function locally so that my quota will not exceed.
I tried running the emulator in my terminal with this command: firebase emulators:start
This initializes all my Cloud Functions except my Firebase Auth and Firebase Storage functions. The firebase docs say the emulator only supports:
HTTPS functions
Callable functions
Cloud Firestore functions
Is there a way/work around so i can run my Firebase Storage and Firebase Auth functions locally?
From the emulator, no. As you've seen, they're just not supported.
Look into invoking functions using the functions shell, or unit testing your functions with the functions-test module.
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 want to send an HTTP Request from Android to a cloud function, post some values, then input these values into real time database.
index.js
const functions = require('firebase-functions');
exports.testPost = functions.https.onRequest((req, res) => {
console.log(req.body);
});
How can I accomplish this?
I see three steps in here:
Calling a Cloud Function from Android.
This is the same as calling any other HTTP URL from Android. See
Calling a Cloud Function from Android through Firebase
Parsing parameters from the call in your Cloud Function
A HTTP triggered Cloud Function is really just an Express handler. So parsing the post works the same as for other Express handlers. The Firebase documentation for HTTP functions has some examples and links to other documentation.
Posting to the database from a Cloud Functions
All the samples in the functions-samples repo include the Firebase Admin SDK. This SDK allows you to access many Firebase features, such as the database, from within your function. But there's also an example in this repo.