firebase-tools: firebase local server can't connect to firebase database?

I’m sorry, I didn’t found answer to that but searched much time.

I don’t know why this simple example works with firebase deploy but not with local server (firebase serve). Should it work normally?

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.test = functions.https.onRequest((req, res) => {
  // This part is totally ignored, and whole function got timeout
  admin.database().ref('chat').once('value').then((snapshot) => {
    res.send({"result": 'OK'})
  });
});

What should I do to test function like this? What I’m missing?

Command: /usr/local/Cellar/node/7.7.1/bin/node /usr/local/bin/firebase serve --debug --only function CLI Version: 3.9.0 Platform: darwin Node Version: v7.7.1

About this issue

  • Original URL
  • State: closed
  • Created 7 years ago
  • Comments: 27 (10 by maintainers)

Most upvoted comments

Hi @stmn This is indeed a bug, thanks for reporting! We are working on the fix. In the meanwhile, here’s a workaround:

  1. Go to https://cloud.google.com/console/iam-admin/serviceaccounts
  2. Click “Create service account”, give it a name (e.g. emulator), give it the Project>Owner role. Check “Furnish a new private key”, pick “JSON”.
  3. Save the file somewhere on your computer
  4. Run export GOOGLE_APPLICATION_CREDENTIALS=”absolute/path/to/file.json”
  5. Run firebase serve --only functions

Another thing you can try is:

  1. Install and initialize gcloud: https://cloud.google.com/sdk/downloads#interactive
  2. Run gcloud auth application-default login in any directory
  3. Run firebase serve --only functions

Hey @stmn thanks for the recording! It seems like you made a service account under a different project than the one you’re running firebase serve with. They have to be the same project, otherwise it doesn’t have the right permissions.

@laurenzlong This bug arised again in v3.9.2 Using the exact snippet from this issue

[2017-08-05T20:21:29.636Z] CLI Version: 3.9.2 [2017-08-05T20:21:29.636Z] Platform: win32 [2017-08-05T20:21:29.636Z] Node Version: v6.11.2 [2017-08-05T20:21:29.636Z] Time: Sat Aug 05 2017 22:21:29 GMT+0200 (W. Europe Daylight Time)

info: User function triggered, starting execution info: Execution took 60009 ms, finished with status: 'timeout' info: Execution took 60019 ms, finished with status: 'crash' Error: Can't set headers after they are sent. at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)

Same functions works fine when deployed, just not local.

edit: Using the gcloud solution mentioned here fixes it.

@laurenzlong I had the same issue. Using gcloud auth fixed it for now. Thanks!

@laurenzlong no luck there either. not sure if its helpful, but for what its worth, I have been able to make calls to admin.auth().createUser and admin.auth().updateUser, its just the ref.once('value') that hangs