Developer-Person
Developer-Person

Reputation: 71

firebase emulator making calls to production firestore instead of emulated firestore

My functions emulator is making calls to the production firestore instead of the firestore emulator. I am not sure how this is happening because I heard that it shouldn't be possible.

I have set up my emulators and the firestore emulator loads:

 Emulator       │ Host:Port      │ View in Emulator UI             │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Authentication │ 127.0.0.1:9099 │ http://127.0.0.1:4000/auth      │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Functions      │ 127.0.0.1:5001 │ http://127.0.0.1:4000/functions │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Firestore      │ 127.0.0.1:8080 │ http://127.0.0.1:4000/firestore │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Hosting        │ 127.0.0.1:5000 │ n/a                             │
├────────────────┼────────────────┼─────────────────────────────────┤
│ Storage        │ 127.0.0.1:9199 │ http://127.0.0.1:4000/storage   │

Here is how I configure the app and firestore in functions:

import { initializeApp } from 'firebase/app';
import {
  getFirestore,
} from 'firebase/firestore';

let firebaseConfig = {
  databaseURL: 'http://localhost:8080?ns=nft-public',
  projectId: 'nft-public',
};

const app = initializeApp(firebaseConfig);

const db = getFirestore(app);

And my emulator options in firebase.json

  "emulators": {
    "auth": {
      "host": "localhost",
      "port": "9099"
    },
    "firestore": {
      "host": "localhost",
      "port": "8080"
    },
    "functions": {
      "host": "localhost",
      "port": "5001"
    },
    "hosting": {
      "port": 5000
    },
    "storage": {
      "port": 9199
    },
    "ui": {
      "enabled": true
    }
  }

Upvotes: 1

Views: 370

Answers (1)

Developer-Person
Developer-Person

Reputation: 71

The reason I was having this issue is because I am a dum dum.

The more technical reason is that I was trying to intialise firebase inside my cloud functions as if it were an external app (which uses the "firebase/app" package).

The correct way to initialise firebase within cloud functions is to use "firebase-admin". This package also contains firestore and all the other functionality you would need.

Upvotes: 1

Related Questions