functions-samples icon indicating copy to clipboard operation
functions-samples copied to clipboard

Ignoring trigger because the Cloud Firestore emulator is not running (firebase serve)

Open AndersonHappens opened this issue 5 years ago • 57 comments

How to reproduce these conditions

Sample name or URL where you found the bug https://github.com/firebase/functions-samples/tree/master/stripe

Failing Function code used (including require/import commands at the top) 'use strict';

const functions = require('firebase-functions'); const admin = require('firebase-admin'); admin.initializeApp(); const logging = require('@google-cloud/logging')(); const stripe = require('stripe')(functions.config().stripe.token); const currency = functions.config().stripe.currency || 'USD';

// [START chargecustomer] // Charge the Stripe customer whenever an amount is written to the Realtime database exports.createStripeCharge = functions.firestore.document('stripe_customers/{userId}/charges/{id}').onCreate(async (snap, context) => { const val = snap.data(); try { // Look up the Stripe customer id written in createStripeCustomer const snapshot = await admin.firestore().collection(stripe_customers).doc(context.params.userId).get() const snapval = snapshot.data(); const customer = snapval.customer_id // Create a charge using the pushId as the idempotency key // protecting against double charges const amount = val.amount; const idempotencyKey = context.params.id; const charge = {amount, currency, customer}; if (val.source !== null) { charge.source = val.source; } const response = await stripe.charges.create(charge, {idempotency_key: idempotencyKey}); // If the result is successful, write it back to the database return snap.ref.set(response, { merge: true }); } catch(error) { // We want to capture errors and render them in a user-friendly way, while // still logging an exception with StackDriver console.log(error); await snap.ref.set({error: userFacingMessage(error)}, { merge: true }); return reportError(error, {user: context.params.userId}); } }); // [END chargecustomer]]

// When a user is created, register them with Stripe exports.createStripeCustomer = functions.auth.user().onCreate(async (user) => { const customer = await stripe.customers.create({email: user.email}); return admin.firestore().collection('stripe_customers').doc(user.uid).set({customer_id: customer.id}); });

// Add a payment source (card) for a user by writing a stripe payment source token to Realtime database exports.addPaymentSource = functions.firestore.document('/stripe_customers/{userId}/tokens/{pushId}').onCreate(async (snap, context) => { const source = snap.data(); const token = source.token; if (source === null){ return null; }

try { const snapshot = await admin.firestore().collection('stripe_customers').doc(context.params.userId).get(); const customer = snapshot.data().customer_id; const response = await stripe.customers.createSource(customer, {source: token}); return admin.firestore().collection('stripe_customers').doc(context.params.userId).collection("sources").doc(response.fingerprint).set(response, {merge: true}); } catch (error) { await snap.ref.set({'error':userFacingMessage(error)},{merge:true}); return reportError(error, {user: context.params.userId}); } });

// When a user deletes their account, clean up after them exports.cleanupUser = functions.auth.user().onDelete(async (user) => { const snapshot = await admin.firestore().collection('stripe_customers').doc(user.uid).get(); const customer = snapshot.data(); await stripe.customers.del(customer.customer_id); return admin.firestore().collection('stripe_customers').doc(user.uid).delete(); });

// To keep on top of errors, we should raise a verbose error report with Stackdriver rather // than simply relying on console.error. This will calculate users affected + send you email // alerts, if you've opted into receiving them. // [START reporterror] function reportError(err, context = {}) { // This is the name of the StackDriver log stream that will receive the log // entry. This name can be any valid log stream name, but must contain "err" // in order for the error to be picked up by StackDriver Error Reporting. const logName = 'errors'; const log = logging.log(logName);

// https://cloud.google.com/logging/docs/api/ref_v2beta1/rest/v2beta1/MonitoredResource const metadata = { resource: { type: 'cloud_function', labels: {function_name: process.env.FUNCTION_NAME}, }, };

// https://cloud.google.com/error-reporting/reference/rest/v1beta1/ErrorEvent const errorEvent = { message: err.stack, serviceContext: { service: process.env.FUNCTION_NAME, resourceType: 'cloud_function', }, context: context, };

// Write the error log entry return new Promise((resolve, reject) => { log.write(log.entry(metadata, errorEvent), (error) => { if (error) { return reject(error); } return resolve(); }); }); } // [END reporterror]

// Sanitize the error message for the user function userFacingMessage(error) { return error.type ? error.message : 'An error occurred, developers have been alerted'; } Steps to set up and reproduce Windows 10, brand new installation of node 10 and firebase-tools (v 6.9.2). Follow the instructions on the github page for installing the npm package and setting up stripe key and so on, but instead of firebase deploy, I used firebase serve. Change node version to 10 in package.json. Attached stripe example to firebase hosting as well.

Sample data pasted or attached as JSON (not an image)

Security rules used

Debug output

Errors in the console logs functions: Using node@10 from host.

  • functions: Emulator started at http://localhost:5001 i functions: Watching "" for Cloud Functions... i hosting: Serving hosting files from: public
  • hosting: Local server: http://localhost:5000 ! Default "firebase-admin" instance created! ! Ignoring trigger "createStripeCharge" because the Cloud Firestore emulator is not running. ! Ignoring trigger "createStripeCustomer" because the Cloud Firestore emulator is not running. ! Ignoring trigger "addPaymentSource" because the Cloud Firestore emulator is not running. ! Ignoring trigger "cleanupUser" because the service "firebaseauth.googleapis.com" is not yet supported.

(node:23520) DeprecationWarning: grpc.load: Use the @grpc/proto-loader module with grpc.loadPackageDefinition instead

Screenshots

Expected behavior

The local emulator starts running and I can test firebase functions without deploying them.

Actual behavior

Firebase hosting works fine, but all cloud firestore functions get ignored.

AndersonHappens avatar May 12 '19 16:05 AndersonHappens

Update: I just tried this on macbook and I got the same results. So it's not a Windows exclusive error. Mac version: Sierra, 10.12.6 npm: 6.4.1 node: 10.15.3 firebase: 6.9.2 firebase-admin: 7.3.0 firebase-functions: 2.3.1 firebase-functions-test: 0.1.6 @google-cloud/logging 0.7.1 stripe: 4.25.0

AndersonHappens avatar May 13 '19 03:05 AndersonHappens

Possibly related to commit #1263: https://github.com/firebase/firebase-tools/pull/1263/commits/e0e58984767da875df4ada4bf1e38d14bd2f4f94

AndersonHappens avatar May 13 '19 21:05 AndersonHappens

@AndersonHappens could be you don't have firestore configured properly in your firebase.json file. This makes emulator not being started.

What you need is to run firebase init firestore in your project directory. This would create firestore rules and indexes files and update your firebase.json correspondingly.

nikitakarpenkov avatar May 27 '19 12:05 nikitakarpenkov

I did try that before but nothing seemed to change.

On Mon, May 27, 2019, 8:57 AM Nikita Karpenkov [email protected] wrote:

@AndersonHappens https://github.com/AndersonHappens could be you don't have firestore configured properly in your firebase.json file. This makes emulator not being started.

What you need is to run firebase init firestore in your project directory. This would create firestore rules and indexes files and update your firebase.json correspondingly.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/firebase/functions-samples/issues/572?email_source=notifications&email_token=ACTN3LTZIL4HYBNZIRAKX7DPXPLD5A5CNFSM4HMKXJL2YY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGODWJXTNY#issuecomment-496204215, or mute the thread https://github.com/notifications/unsubscribe-auth/ACTN3LQXFWYOLQVVUU6RT33PXPLD5ANCNFSM4HMKXJLQ .

AndersonHappens avatar May 27 '19 15:05 AndersonHappens

I have the same exact issue. Found any fix?

adxworld avatar Jun 20 '19 00:06 adxworld

No fixes found for me. I could still deploy the functions just fine (I just couldn't emulate them), so I deployed them while having them pointed to a fake test database path and tested them locally with that fake path, and then when I deployed the website for real I changed the paths in both the functions and in my hosting project to point towards the actual database.

AndersonHappens avatar Jun 20 '19 01:06 AndersonHappens

I changed the serve script in my functions package.json to npm run build && firebase serve --only functions,firestore and it recognizes both my HTTP triggers and Cloud Firestore Triggers.


Edit: Moving my other comment here for visibility with this issue

So this isn't working like I thought.

Running serve ("serve": "npm run build && firebase serve --only functions") I get the following message for any Firestore triggers:

Ignoring trigger "ordersCreate" because the Cloud Firestore emulator is not running.

Switching the serve command to npm run build && firebase serve --only functions,firestore I get the following message for any Firestore triggers:

Trigger "ordersCreate" has been acknowledged by the Cloud Firestore emulator.

The emulator now works, but...

Unfortunately, this still means the triggers aren't initialized so I can't test them locally and see any console logs or other debugging information.

BrentWMiller avatar Jun 20 '19 13:06 BrentWMiller

@AndersonHappens Well, thats exactly what I have been doing for a long while.

adxworld avatar Jun 20 '19 19:06 adxworld

@BrentWMiller I gotta try it. Will get back to you after I did.

adxworld avatar Jun 20 '19 19:06 adxworld

I've been messing with this for the last few hours.

Running firebase serve --only functions,firestore prompts me to setup the firestore emulator, which I thought I had done, but apparently hadn't.

Running firebase setup:emulators:firestore and then firebase serve --only functions,firestore again then told me that my stripe token was undefined, but it was clearly defined using firebase functions:config:get, so I followed the advice in this thread: https://github.com/firebase/firebase-tools/issues/678 and did
firebase functions:config:get > .runtimeconfig.json and that got rid of that error. (Though the thread references a page for performing that command, but I can't find any references to that command anywhere in the actual documentation).

If I then try to test everything at once with firebase serve --only hosting,functions,firestore, then it tells me that Trigger "x" has been acknowledged by the Cloud Functions emulator for most of my functions.

However, if I just call firebase serve it still gives me the "Ignoring trigger because the Cloud Firestore emulator is not running" warning, even if I am running the firebase emulators in a different console window and have run export FIRESTORE_EMULATOR_HOST=localhost:8080, but that is really whatever, calling all of the things I want explicitly isn't that much of a pain.

The real issue I have now is that the firestore emulator doesn't seem to do anything! If I use the firebase website created by hosting to update firestore, it updates the actual database and calls the actual firestore function which gets logged in the firebase logs. If I change the local index.js to add different logs, those logs don't appear and only the old ones are used. It does not log the console.log calls to the terminal like the documentation says or even to the debug file.

At this point I feel like I'm missing something obvious or that the emulator is not designed to work like I think it does. Is there some flag or command I am missing so that hosting sends the data to the emulator instead of the actual database or does the emulator not intercept the database writes from firebase hosting?

AndersonHappens avatar Jun 20 '19 23:06 AndersonHappens

Same problem. Tried using firebase emulators:start to start hosting, firestore, functions. The idea was same op, to test triggers on a database. Also tried with firebase serve no go. Same error in all, updates the actual database and even though it reports as "acknowledged" by emulator the functions does not trigger "onCreate" and so on....

functions package:
  "firebase-admin": "^8.0.0",
  "firebase-functions": "^3.0.0",
  "glob": "^7.1.4"

mainproject:
  "firebase": "^6.2.0",

$ firebase --version
7.0.0

kreoo avatar Jun 21 '19 17:06 kreoo

I tried again today. Functions are being acknowledged by the emulator, but the modified functions are not being used and only the deployed functions are. Additionally, no logging is happening in console.

node version: 8.13.0

firebase version: 7.1.1 firebase-admin: 8.2.0 firebase-functions: 3.1.0

I also tried updating node to 12.6.0 and npm to 6.9.0, but that didn't change anything.

AndersonHappens avatar Jul 19 '19 23:07 AndersonHappens

Same here

lincolnlemos avatar Jul 20 '19 19:07 lincolnlemos

Same here, any update?

dreamdev21 avatar Jul 23 '19 04:07 dreamdev21

when will this be fixed please?

digitalml avatar Jul 23 '19 16:07 digitalml

Same here too, are there are any other open issues related to this ?

SamalaSumanth0262 avatar Jul 26 '19 13:07 SamalaSumanth0262

firebase serve did not work, firebase serve --only functions,firestore did work for me on Node 10.

"firebase": "^6.3.1",
"firebase-admin": "^8.3.0",
"firebase-functions": "^3.0.2"

danieldanielecki avatar Jul 28 '19 12:07 danieldanielecki

@danieldanielecki did you test the firestore functions for firebase serve --only functions,firestore and see if they printed to your local console? Did you test to see if it was running your local code (and not your deployed code)? Because for me it only ever runs the deployed code and not the local code I'm trying to serve

AndersonHappens avatar Jul 31 '19 18:07 AndersonHappens

All emulators start for me, however I receive this message for my auth onCreate trigger:

functions[auth_onCreate-default]: function ignored because the firebaseauth.googleapis.com emulator does not exist or is not running.

skwny avatar Aug 03 '19 02:08 skwny

Same here, I can't my triggers to work locally via the emulators.

dorian-marchal avatar Aug 26 '19 16:08 dorian-marchal

This is killing me slowly. Fixed it a couple of days ago, and bam, same error again?! Cannot seem to work out why.

jmdibble avatar Aug 26 '19 19:08 jmdibble

this bug is ruining my life. it makes testing database sharding almost impossible.

georgewwindsor avatar Sep 07 '19 12:09 georgewwindsor

Any updates on this error? Same issue package.json "serve": "npm run build && firebase serve --only functions,firestore" console output: functions[createUserRecord]: function ignored because the firebaseauth.googleapis.com emulator does not exist or is not running.

super-jb avatar Sep 23 '19 17:09 super-jb

I am facing the same issue, I am trying to send email "welcome email" but it is not working, any suggestion is much appreciated

export const sendWelcomeEmail = functions.auth.user().onCreate((user) => {
    console.info("sendWelcomeEmail", user);
});

I get the following In the console functions[sendWelcomeEmail]: function ignored because the firebaseauth.googleapis.com emulator does not exist or is not running.

UPDATE This does work when deployed. It only does not work locally. Anyone stuck on development don't be discouraged , just deploy and test it live

sidzan avatar Sep 24 '19 03:09 sidzan

Together with this issue, the emulators are unusable on windows:

https://github.com/firebase/firebase-tools/issues/1458

barnu5 avatar Oct 02 '19 09:10 barnu5

Guys this is my first major firebase project and I'm kinda stuck, same issue, deploying functions takes forever and this was my only sane option :/ Is the only solution is dual booting Linux or getting a mac? I really like the tech but the dev experience I'm getting is not smooth at all...

Galzk avatar Oct 02 '19 15:10 Galzk

@Galzk you can also git-clone, edit, and deploy your project from the Google Cloud Shell of your Firebase account: https://ssh.cloud.google.com/cloudshell/editor

laurentpayot avatar Oct 03 '19 07:10 laurentpayot

@Galzk you can also git-clone, edit, and deploy your project from the Google Cloud Shell of your Firebase account: https://ssh.cloud.google.com/cloudshell/editor

Thank you for that, trying now

Galzk avatar Oct 03 '19 11:10 Galzk

I just spent 4 hours trying to resolve this issue. Ended up shotgunning the onCreate event straight to production after trying everything I found here and on stackoverflow. I'm still getting the error: functions[createUserDoc]: function ignored because the firebaseauth.googleapis.com emulator does not exist or is not running.

stephan227 avatar Oct 14 '19 16:10 stephan227

Had similar problem with firestore emulator. Was resolved after I run

firebase init

again, selecting database and functions. Maybe same will help for other types of emulators.

iamboroda avatar Oct 21 '19 20:10 iamboroda