Repository navigation
Description
I have a project using firestore, functions and hosting.
Until recently I was happily working locally with firebase serve --only functions,hosting and that would work with the production firestore.
Using the new firestore emulator I read loads suggesting to use emulator over serve, and my functions have firestore triggered functions which I would like to test locally.
When I firebase serve --only functions I get a Ignoring trigger "onDocumentCreate" because the Cloud Firestore emulator is not running. This suggests the trigger can work with the emulator.
When I have the firestore emulator running in another process, the functions serve does not detect the firestore emulator running.
So finally I try firebase emulators:start --only firestore,functions which starts up fine with all triggers.
As soon as Firestore code is executed I get the error:
Google API requested!
- URL: "https://oauth2.googleapis.com/token"
- Be careful, this may be a production service.
> { Error: Getting metadata from plugin failed with error: Header field "authorization" must have only a single value
> at Http2CallStream.call.on (/home/dav/dev/lms/functions/node_modules/@grpc/grpc-js/build/src/call.js:68:41)
> at emitOne (events.js:121:20)
> at Http2CallStream.emit (events.js:211:7)
> at process.nextTick (/home/dav/dev/lms/functions/node_modules/@grpc/grpc-js/build/src/call-stream.js:71:22)
> at _combinedTickCallback (internal/process/next_tick.js:132:7)
> at process._tickCallback (internal/process/next_tick.js:181:9)
> code: 'ERR_HTTP2_HEADER_SINGLE_VALUE',
> details: 'Getting metadata from plugin failed with error: Header field "authorization" must have only a single value',
> metadata: Metadata { options: undefined, internalRepr: Map {} } }
How can I make firebase-admin work with emulated firestore and functions?
import * as admin from 'firebase-admin';
import * as functions from 'firebase-functions';
admin.initializeApp();
const firestore = admin.firestore();
const foo = firestore.collection('colName').doc('docId')
.onSnapshot(snap => {
console.log(snap);
});
Using all the latest packages:
"firebase-admin": "^8.1.0",
"firebase-functions": "^3.0.1",
"firebase-tools": "^7.0.0",
OS: Linux Ubuntu 18.04
NPM version: 6.9.0
Node version: 8.15.0
Activity
google-oss-bot commentedon Jun 19, 2019
I found a few problems with this issue:
schmidt-sebastian commentedon Jun 19, 2019
@ChromeQ The easiest way to achieve this to use the recent version of
@google-cloud/firestore. Starting with version 2.2, we support the environment variableFIRESTORE_EMULATOR_HOST. If you set this environment variable to the host entry provided at emulator startup (such aslocalhost:8080), Firestore will connect to the emulator instead of to the backend.ChromeQ commentedon Jun 19, 2019
Thanks, but that also didn't work as I get another problem:
npx firebase emulators:start --only firestoreruns in one process and showsFIRESTORE_EMULATOR_HOST=localhost:5002Then in a new process I run:
FIRESTORE_EMULATOR_HOST=localhost:5002 npx firebase serve --only functionsschmidt-sebastian commentedon Jun 19, 2019
We will get that fixed! Sorry for the trouble.
ChromeQ commentedon Jun 19, 2019
Thanks, but ideally it would work with simply:
firebase emulators:start --only firestore,functionsorfirebase serve --only firestore,functionsCan it be fixed to work like that without the need of the extra variable or extra process running firestore emulator?
Thanks
hiranya911 commentedon Jun 21, 2019
@schmidt-sebastian where did we land with this one?
12 remaining items
ryanpbrewster commentedon Aug 26, 2019
That's clever. Thanks for posting this!
The alternative is to set the environment variable outside of the script itself.
We have documentation in the works for all of these SDKs. There are quite a few of them, and we wanted to make sure the experience was similar across the board. Be on the lookout for comprehensive docs on using the Firebase emulator suite.
Elyx0 commentedon Nov 8, 2019
I'm hitting the same problem here
with
firebase emulators:start --only functions,database,firestoreBut then any code calling
adminthrows withCan't determine Firebase Database URL.I run my tests with
I see from a4ad828 that one of the env override got removed, am I missing something?
Elyx0 commentedon Nov 8, 2019
With
"@firebase/testing";it sets it up correctlyThe problem is that most of my code relies on
in each file for doing the database management, so I can't mock it
Should I add a wrapper around my
adminthat exports the one fromfirebase/testingif in test mode to my functions and another to live mode from thefirebase-admin? It feels contrived since I have a correct emulator running...Elyx0 commentedon Nov 8, 2019
I ended up doing something like that:
But it feels like an ugly hack
akauppi commentedon Jun 25, 2020
Though this is closed, this may help someone:
If you're wanting to use the Admin SDK with the new Emulator UI, make sure you set up
projectIdparameter to match your Firebase project. Otherwise, data gets written somewhere, but isn't visible in the emulator.milan-codes commentedon Jul 12, 2020
Still facing this issue, even though the FIREBASE_DATABASE_EMULATOR_HOST env variable is set (as it is stated here), I still can't get Admin SDK to connect to the emulators. Tried @akauppi 's suggestion, but still no success.
schmidt-sebastian commentedon Jul 14, 2020
@milan-codes Most of the advice here is for the Firestore emulator.
There is currently a known bug with the Database emulator setting: firebase/firebase-js-sdk#3144
Do you see errors similar to the ones reported there?
milan-codes commentedon Jul 14, 2020
@schmidt-sebastian Thanks for pointing me to the right issue! That issue seems to be similar to mine, but now I had the chance to test my code on a Windows PC. Everything works fine there, it successfully connects to the emulator. I have no idea why it doesn't work on my mac. Should I open a new issue for this?
schmidt-sebastian commentedon Jul 15, 2020
#3144 is merged and should be released this week. I suggest to wait a couple days before trying to repro.
habibKadiri commentedon Nov 17, 2020
You are a god send, thank you good sir!
ir-fuel commentedon Mar 11, 2021
FYI @firebase/testing has been deprecated
lookfirst commentedon Mar 11, 2021
what's the replacement?
schmidt-sebastian commentedon Mar 16, 2021
@lookfirst https://www.npmjs.com/package/@firebase/rules-unit-testing
vladfaust commentedon Jul 27, 2021
Please answer if current release supports managing multiple tenants with Emulators? I currently get such a error:
for any tenant-related activity, such as
app.auth().tenantManager().listTenants().I tried everything: setting
databaseURL,process.env.FIREBASE_AUTH_EMULATOR_HOST = "localhost:9099"andprocess.env.FIRESTORE_EMULATOR_HOST = "localhost:8080". Still no luck.Thanks in advance!
UPD: I tried to do the following via Postman:
GET http://localhost:9099/identitytoolkit.googleapis.com/v2/projects/<project name>/tenants; still getting the 404 response.SUVOLewis commentedon Aug 3, 2021
@vladfaust When trying to create a new user under a specific tenant, I get the following:
I think it's safe to assume that the Google Cloud Identity Platform Tenant System is not yet a part of the Firebase Auth Emulator.
Currently, I'm unaware of any alternatives to this.