Skip to content

[Firestore] Can not connect to both production and local emulator database #1839

Closed
@danielo515

Description

@danielo515

[REQUIRED] Step 1

Yes, I think this is the right place

[REQUIRED] Step 2: Describe your environment

  • Operating System version: MacOS
  • Firebase SDK version: 11.0.0
  • Firebase Product: Firestore
  • Node.js version: 16
  • NPM version: yarn

[REQUIRED] Step 3: Describe the problem

Steps to reproduce:

Please take a look at the attached code in the next section.
When you try to run that code you will get an error saying that TypeError: this.ensureApp(...).firestore is not a function. Seems that the initializeApp method is not returning an actual app instance that includes everything.

I want to be able to connect both to my production app and to my local emulator suite so I can do some dumps from one to the other.

admin.firestore.setLogFunction(console.log);

This will print Firestore logs to the console.

Relevant Code:

import { cert, initializeApp, applicationDefault } from 'firebase-admin/app';
import * as admin from 'firebase-admin';
import authData from '../admin-sdk.json';
import { Firestore } from 'firebase-admin/firestore';
import { emulators } from '../firebase.json';

initializeApp({
  // the cert accepts either an string or an object, but it si wrongly typed
  credential: cert(authData as unknown as string),
});

const localApp = initializeApp(
  { credential: applicationDefault(), databaseURL: `localhost:${emulators.firestore.port}` },
  'local',
);

export const db = admin.firestore();
export const localDb = admin.firestore(localApp);

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions