You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Connect to firestore emulator with admin SDK requires supplying the project-id #2602
The firebase admin SDK must init with some project-id, but it doesn't have to be a real one.
Meanwhile, if you also want to visualize the db using the emulator UI, or if you're using the function emulator at the same time, then the emulator must start with the same project-id as the admin SDK. This can be achieved with firebase --project=some_project_id emulators:start. Without the flag, the emulator will use the project id in your .firebaserc, and create a different instance of db running parallel with the admin SDK's instance.
===========
Original issue:
I found that when I have a firestore running on local emulator, and I want to directly access it with a script using the admin SDK, I need to supply the project-id to the admin SDK, which I believe is not the intended behavior.
I use the nodejs firebase-admin package, not sure if the bug is theirs.
Hi @markgoho , thanks, your snippet was the same as mine (at the bottom of my post).
The issue is not getting it to work, the issue is that project-id shouldn't be required. This code is very likely for the purpose of unit testing, and 1. I may change the project id with firebase use, and 2. I may not want to commit my project-id into source control for open source projects, which is why .firebaserc is a separate file from firebase.json.
@ZYinMD this is the intended behavior, without some project ID (does not have to be a real one!) the Firestore Admin SDK can't construct the right calls to the Firestore API, or in this case the Firestore Emulator.
If you want to set this without modifying source code, use the GCLOUD_PROJECT environment variable which is respected by all Cloud SDKs.
Hi @samtstern , thanks! Should it be mentioned in the doc here?
Also, about "project ID doesn't need to be a real one" that you mentioned: I tried to set it to 'foo', and indeed, it still works, which blew my mind. Should it also be mentioned in the doc?
@ZYinMD just FYI if you're using the Firestore and Functions emulators together make sure they have the same project ID! Otherwise triggers won't fire, etc.
@samtstern to clarify: the emulator reads the real project-id from .firebaserc, and the admin-sdk running with the functions is also part of the emulator, which also reads the same project-id, which has no issues. However, the admin-sdk in the test suites mentioned in the OP is manually given a project-id, and you're saying this one should also be the same one, which is the real id, correct?
Hi @samtstern, I see that you have updated the doc since the discussion above.
I just found that using a fake project-id has more problems than I thought - the documents inserted into the db with a fake project-id won't show up in the emulator UI, which means the setup of a fake-project id + admin-sdk + emulator for prototyping is pointless.
You might want to make it more clear in the doc, the way it's written now is a little misleading because it advertises something that sounds great, but don't really work.
It's also worth noting that this setup is still usable in unit testing, because the tests don't involve the emulator UI. However, the test becomes flaky when it involves both directly accessing the db and accessing the db via functions, because the functions will always use the real project-id stored in firebaserc.
@ZYinMD Your comment here lead me to realise that the project-id relates to the project being used by firebase use. I was completely unaware that this was the case and was struggling to understand how the project-id had any relevance given that the docs say you can use anything you want. Thank you!
So is there no way to view data in the Firestore emulator UI while using a fake project id? Right now the data only displays in the UI when I use the project ID that matches the project which I set via firebase use which means I have to use my real project id...
You can always start the Firebase CLI using a fake project ID to match. Use
`firebase —project=<whatever> emulators:start`
But yes you do need to use the same project ID everywhere
The following initialization seemed to allow the admin SDK to use the firestore emulator but it only allowed partial use of the auth emulator (calls to admin.auth().listUsers() returned an empty array despite there being at least one emulated user):
import * as admin from 'firebase-admin'
admin.initializeApp({ projectId: process.env.GCLOUD_PROJECT});
const Firestore = admin.firestore();
Firestore.settings({
host: "localhost:8080",
ssl: false
});
Currently using:
firebase-tools: v9.4.0
npm firebase-admin package v9.5.0
Reacted by Will, Amir Muhammad Hakim, ssathya96, Michael Rhee, Christian Genco, Nicholas Stephan and Pavel_Vynychenko
I faced a similar problem. I was using two different projects, one for development another for production.
There are many places where the project ID is used, and a mismatch can yield weird bugs that can be hard to troubleshoot:
.firebaserc on your project directory
the admin.initializeApp() call
the url used to call the functions (HTTP 404 when wrong)
Activity
markgoho commentedon Sep 7, 2020
try
ZYinMD commentedon Sep 7, 2020
Hi @markgoho , thanks, your snippet was the same as mine (at the bottom of my post).
The issue is not getting it to work, the issue is that project-id shouldn't be required. This code is very likely for the purpose of unit testing, and 1. I may change the project id with
firebase use, and 2. I may not want to commit my project-id into source control for open source projects, which is why.firebasercis a separate file fromfirebase.json.samtstern commentedon Sep 8, 2020
@ZYinMD this is the intended behavior, without some project ID (does not have to be a real one!) the Firestore Admin SDK can't construct the right calls to the Firestore API, or in this case the Firestore Emulator.
If you want to set this without modifying source code, use the
GCLOUD_PROJECTenvironment variable which is respected by all Cloud SDKs.ZYinMD commentedon Sep 9, 2020
Hi @samtstern , thanks! Should it be mentioned in the doc here?
Also, about "project ID doesn't need to be a real one" that you mentioned: I tried to set it to 'foo', and indeed, it still works, which blew my mind. Should it also be mentioned in the doc?
samtstern commentedon Sep 10, 2020
@ZYinMD I will update the docs, thanks!
samtstern commentedon Sep 10, 2020
@ZYinMD just FYI if you're using the Firestore and Functions emulators together make sure they have the same project ID! Otherwise triggers won't fire, etc.
ZYinMD commentedon Sep 11, 2020
@samtstern to clarify: the emulator reads the real project-id from
.firebaserc, and the admin-sdk running with the functions is also part of the emulator, which also reads the same project-id, which has no issues. However, the admin-sdk in the test suites mentioned in the OP is manually given a project-id, and you're saying this one should also be the same one, which is the real id, correct?samtstern commentedon Sep 15, 2020
@ZYinMD sorry for the slow response but yes that's exactly it.
vajahath commentedon Oct 8, 2020
@samtstern If you could also add a single line code to that documentation, that would be very nice. like:
samtstern commentedon Oct 9, 2020
@vajahath will do!
ZYinMD commentedon Oct 14, 2020
Hi @samtstern, I see that you have updated the doc since the discussion above.
I just found that using a fake project-id has more problems than I thought - the documents inserted into the db with a fake project-id won't show up in the emulator UI, which means the setup of a fake-project id + admin-sdk + emulator for prototyping is pointless.
You might want to make it more clear in the doc, the way it's written now is a little misleading because it advertises something that sounds great, but don't really work.
It's also worth noting that this setup is still usable in unit testing, because the tests don't involve the emulator UI. However, the test becomes flaky when it involves both directly accessing the db and accessing the db via functions, because the functions will always use the real project-id stored in
firebaserc.samtstern commentedon Nov 2, 2020
@ZYinMD we're working on showing multiple databases in the Firestore Emulator UI, you can follow that here:
firebase/firebase-tools-ui#281
JamieDixon commentedon Jan 20, 2021
@ZYinMD Your comment here lead me to realise that the
project-idrelates to the project being used byfirebase use. I was completely unaware that this was the case and was struggling to understand how theproject-idhad any relevance given that the docs say you can use anything you want. Thank you!MchlG commentedon Jan 27, 2021
So is there no way to view data in the Firestore emulator UI while using a fake project id? Right now the data only displays in the UI when I use the project ID that matches the project which I set via
firebase usewhich means I have to use my real project id...samtstern commentedon Jan 27, 2021
thMthMnky commentedon Mar 10, 2021
With the following environment variables:
The following initialization seemed to allow the admin SDK to use the firestore emulator but it only allowed partial use of the auth emulator (calls to admin.auth().listUsers() returned an empty array despite there being at least one emulated user):
Currently using:
firebase-tools: v9.4.0
npm firebase-admin package v9.5.0
okric commentedon Jun 20, 2021
I faced a similar problem. I was using two different projects, one for development another for production.
There are many places where the project ID is used, and a mismatch can yield weird bugs that can be hard to troubleshoot:
More?
samtstern commentedon Jun 21, 2021
@RicardoAratani we are working on a plan to make it much more obvious when you have a project ID mismatch and also make it easier to set things up!