firebase-tools icon indicating copy to clipboard operation
firebase-tools copied to clipboard

Firestore data in emulator not showing up in emulator UI

Open metachris opened this issue 6 years ago • 22 comments

[REQUIRED] Environment info

firebase-tools:

$ firebase --version
8.4.2

$ node -v
v10.16.3

Platform: macOS

[REQUIRED] Test case

index.js

const admin = require('firebase-admin');

process.env.FIRESTORE_EMULATOR_HOST = 'localhost:8080';

admin.initializeApp();

let db = admin.firestore();

async function test() {
    let docRef = db.collection('users').doc('alovelace2');

    let setAda = docRef.set({
        first: 'Ada',
        last: 'Lovelace',
        born: 1815
    });
}

test()

package.json

{
    "name": "firebase-emulator-ui-test",
    "version": "1.0.0",
    "main": "index.js",
    "author": "Chris Hager <[email protected]>",
    "license": "MIT",
    "dependencies": {
    "firebase-admin": "^8.12.1"
    }
}

I simply created a new directory, initialise firebase with firebase init and the node project with yarn init, and added firebase-admin (yarn add firebase-admin).

[REQUIRED] Steps to reproduce

  • Run the code above. Adding multiple entries to the firestore works, and I can retrieve them in code.

[REQUIRED] Expected behavior

Firebase UI at http://localhost:4000/firestore should show the data.

[REQUIRED] Actual behavior

Firebase UI at http://localhost:4000/firestore shows only an empty root.

metachris avatar Jun 15 '20 09:06 metachris

Found the answer -- when calling admin.initializeApp() you need to set the correct projectId, then the data will show up.

metachris avatar Jun 15 '20 10:06 metachris

To add to this, I got the same issue when creating different project environments via firebase use [your environment name]. The local emulator was working, just not showing any data. Once I switched my config settings to the correct environment, all was well and data started showing up in the emulator.

gordonjl avatar Jun 17 '20 19:06 gordonjl

Found the answer -- when calling admin.initializeApp() you need to set the correct projectId, then the data will show up.

Embarrassing that this isn't in the documentation more clearly.

basickarl avatar Nov 13 '20 14:11 basickarl

@basickarl calling something embarrassing isn't very constructive, also this IS in the documentation:

Link: https://firebase.google.com/docs/emulator-suite/connect_firestore#admin_sdks

Screenshot: Screen Shot 2020-11-13 at 2 18 24 PM

samtstern avatar Nov 13 '20 14:11 samtstern

@samtstern Well that is absolutely delightful for a person who knows where to find it. Google documentation together with Microsoft has always been a wall-of-text mess.

basickarl avatar Nov 13 '20 14:11 basickarl

My script does set the projectId, and I still have this issue. I am running firebase 8.16.2.

My scripts are reading from and writing to the emulator fine, but if I go into the Emulator UI, there is no data shown. I can only view the collections, documents, subcollections, etc. if I manually create the top-level collection and documents. If I use the Cloud Firestore without the emulator, the same scripts work fine and I can view all the data in the UI without any manual intervention. Please advise.

darlenew avatar Nov 14 '20 23:11 darlenew

@samtstern - In my case, I'm pretty sure that this error is the result of attempting to use a fake id (intentionally or accidentally). When I initialize with the same id as firebase use, everything works; when using a fake ID:

  • ✓ Read/write works
  • ✘ No data in Emulator UI
  • ✘ No CF trigger console.log in shell (I didn't verify if they fire)
  • ✘ Cannot auth from client SDK

Based on the docs and your comments, I'm not convinced it's even possible.

From the docs you quoted:

Note that you do not need to use your real Firebase project ID

However, over here you said:

@cbdeveloper yes that's correct, the emulator suite emulates the currently active project as determined by firebase-tools. That's the project which will be shown in the Emulator UI and that's also the project which will be used when configuring Cloud Functions triggers locally.

To my knowledge, the current project is set using firebase use; when I attempt this with a fake id, I get this error:

firebase use <FAKE_ID>
Error: Invalid project selection, please verify project <FAKE_ID> exists and you have access.

So how is it possible to set a fake id for firebase-tools, and thus for the emulator?

charles-allen avatar Dec 22 '20 07:12 charles-allen

@charles-allen you've got it exactly right. A fake project ID will allow database writes but not much else. If you want to start the Emulator Suite with a fake project ID just do this:

firebase --project="foobar123" emulators:start

samtstern avatar Dec 22 '20 11:12 samtstern

@charles-allen you've got it exactly right. A fake project ID will allow database writes but not much else. If you want to start the Emulator Suite with a fake project ID just do this:

firebase --project="foobar123" emulators:start

Thanks! That was the missing piece of the puzzle.

Can I suggest (or even PR) to add the above to the docs here (where it mentions setting the fake project ID; since the snipper only shows the Admin SDK side, which doesn't fully work without the above).

I actually Googled quickly, and I can't find any reference to --project in the docs at all (only on other sites).

charles-allen avatar Dec 22 '20 17:12 charles-allen

@charles-allen I wish we could accept a docs PR but we're not open source yet, I will get to work on a clarification!

samtstern avatar Dec 22 '20 17:12 samtstern

Just for people having this problem (because I had): I really had to add --project {projectId} to the command for the data to show up in the dashboard. So firebase emulators:start --project {projectId}

loolooii avatar Nov 08 '21 22:11 loolooii

I'm having this problem and I did not create any environments with firebase use. The data stopped showing up after I used the --export-on-exit and --import options for the first time, but omitting these now doesn't make the data reappear.

I can connect to, read and write from the database just fine, and the export contains everything as intended.

Specifying --project on startup doesn't have any effect.

Lootwig avatar Dec 06 '21 09:12 Lootwig

I am using docker and have never gotten data to load, even with admin.initilizeApp using credentials for my existing project, firebase use my-project etc. (also doesn't work without docker and just using firebase --project project-id emulators:start, no data in ui)

qualle-admin avatar Jan 27 '22 01:01 qualle-admin

how was this issue solved? I am still facing it

FrancescoBurchielli avatar May 15 '22 19:05 FrancescoBurchielli

I had the same problem. admin.initializeApp( {projectId: "project-XXXX-XXXXX" } ) did not fix the problem. What fixed it for me was starting the emulator like this with your own project id: firebase emulators:start --project project-XXXX-XXXXX

rkhpedersen avatar Aug 26 '22 13:08 rkhpedersen

same issue - nothing is working

jpiabrantes avatar Sep 16 '22 15:09 jpiabrantes

Adding my findings here. I was basically at the same place as this comment https://github.com/firebase/firebase-tools/issues/2365#issuecomment-749396838 and had resigned to use a real project id when running the emulator so that the active project in the CLI (firebase use) would match admin.initializeApp({ projectId: xx }) and firebase --project project-id emulators:start.

However, as stated in the Emulator docs:

For any products you are not emulating, your apps and code will interact with the live resource (database instance, storage bucket, function, etc.).

This felt really bad tbh, a loaded gun waiting to go off when someone thinks they're working against an emulator and does a mistake that mutates real data.

However what the docs also says is interesting:

Demo: A demo Firebase project has no real Firebase configuration and no live resources. These projects are usually accessed via codelabs or other tutorials.

When working with demo Firebase projects, your apps and code interact with emulators only. If your app attempts to interact with a resource for which an emulator isn't running, that code will fail.

Project IDs for demo projects have the demo- prefix.

This was news to me. Turns out if you use the demo- prefix in your non-existing, fake project id, it won't matter whatever is active in the CLI (firebase use).

The final state that got everything working for me:

  • Start emulators: npx firebase --project=demo-eaardal-emulator emulators:start"
  • The command running the script using firestore (npm run generate:local):
"generate:local": "env-cmd --file=.env.local ts-node ./src/generate.ts",

.env.local:

FIRESTORE_EMULATOR_HOST=localhost:8080
PROJECT_ID=demo-eaardal-emulator
NODE_ENV=development
  • In code (generate.ts, writing data to Firestore), using the JavaScript firebase-admin SDK:
import { initializeApp } from "firebase-admin/app";
const app = initializeApp({ projectId: process.env.PROJECT_ID });
// emulator is enabled by setting FIRESTORE_EMULATOR_HOST when using firebase-admin, other SDKs might set it up differently

Data is showing in the Firestore Emulator UI both when using --export-on-exit ./localdata --import ./localdata and not.

Just to verify, I have a real test project set as my active CLI project (firebase use {real test project}) and can verify that if I try to use a non-emulated service that exists and is active in the real test project, it will simply fail and not fall back on using the real project.

Hope this helps!

eaardal avatar Oct 13 '22 07:10 eaardal

All the cases above didn't work for me, so I had to generate a new app firebaseConfig and re-initialize the firebase services, then the data showed in the UI.

lagupa avatar May 14 '23 00:05 lagupa

How did you generate a new firebaseconfig? and re-initilize, am stuck on this since 5 in the morning, no online documentation has helped. my data does not show on the emulator ui.

IncognitoGK9 avatar May 24 '23 09:05 IncognitoGK9

This helped me: https://stackoverflow.com/a/72839398/14116068

charlestbell avatar Sep 30 '23 20:09 charlestbell