0
votes

I have a firebase project which has a project id and data in firestore. The app works great on the web, android studio, actual device etc to connect to my project firestore documents.

I want to create a cloud function so installed the firebase CLI, created a function but when i want to read data it will only access collections created at http://localhost:4000/firestore.

I have tried creating a serviceAcount from my firebase console.

 const admin = require('firebase-admin');
  var serviceAccount = require("./serviceAccountKey.json");

  admin.initializeApp({
                       credential: admin.credential.cert(serviceAccount)
                     });

I have tried using my known working firebaseConfig:
   

 const admin = require('firebase-admin');
    firebaseConfig = {
                      apiKey: "***************",
                      authDomain: "**********.firebaseapp.com",
                      projectId: "**********-****",
                      storageBucket: "*******-******.appspot.com",   
                      messagingSenderId: "********",
                      appId: "1:***********:web:*************"
                    };
  admin.initializeApp(firebaseConfig);

I have read everything (probably not) out there over the last couple days. It took me forever to even understand why the db.collection request was not returning anything, before i realized it was looking in another firestore bucket at http://localhost:4000/firestore (i created a document, and the cloud function immediately returned a working result). I thought I had figured it out.

How do i point the emulator to my existing firestore project data. I am clearly missing something really basic in understanding.

1

1 Answers

0
votes

After debugging and thinking about it I realized there had to be something very basic:

firebase emulators:start --only functions

When i started the emulator with --only functions it gave me a warning that production data would be used and as thick as I am the penny dropped. Make sure to specify --only functions which precludes the emulated firestore on localhost:4000.

It was probably so obvious no one anywhere actually stated it or what happens if you omit --only functions.