5

I've been trying to implement some Cloud Functions that reads data from Cloud Firestore. I want to test these locally, however I cannot get the Cloud Firestore emulator to run. My assumptions are that it's where the problem is, but it might be somewhere else - if that's the case please let me know.

Here is my function:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'

admin.initializeApp()

//Functions to fetch the documents in the user collection and send them to the client.
export const getUserDocs = functions.https.onRequest((request, response) => {
    admin.firestore().doc('/Users/{documentId}').get()
    .then(snapshot => {
        const data = snapshot.data()
        response.send(data)
    })
    .catch(error => {
        response.status(500).send(error)
    })
})

This is the error I get when I run the command firebase serve --only functions :

Carlo@Carlos-Air-2 functions % firebase serve --only functions
⚠  Your requested "node" version "8" doesn't match your global version "13"
✔  functions: functions emulator started at http://localhost:5000
i  functions: Watching "/Users/Carlo/app/functions" for Cloud Functions...
✔  functions[getUserSports]: http function initialized (http://localhost:5000/app/us-central1/getUserSports).
i  functions: Beginning execution of "getUserSports"
⚠  functions: The Cloud Firestore emulator is not running, so calls to Firestore will affect production.
⚠  External network resource requested!
   - URL: "http://179.252.163.233/computeMetadata/v1/instance"
 - Be careful, this may be a production service.
⚠  External network resource requested!
   - URL: "http://metadata.google.internal./computeMetadata/v1/instance"
 - Be careful, this may be a production service.
i  functions: Finished "getUserSports" in ~9s

I've tried starting from scratch again and uninstalling and reinstalling but with no success. I've also watched the tutorial series on YouTube but for some reason my code doesn't work.

Any help is greatly appreciated.

Doug Stevenson
  • 297,357
  • 32
  • 422
  • 441
carloregian
  • 65
  • 1
  • 5
  • 1
    you can start both functions and database with this https://tianhaoz.com/eng/mobile/flutter/#test-with-firebase-emulator – Tianhao Zhou Apr 16 '20 at 07:26

2 Answers2

5

I was getting the following error when trying to run firebase for local testing:

The Cloud Firestore emulator is not running, so calls to Firestore will affect production.

What I did wrong was no select both Firestore and Functions (see image).

then there are a number of commands available to spin up local testing:

  1. firebase serve
  2. firebase serve --only functions
  3. firebase emulators:start --only=functions
  4. firebase emulators:start

The last one (#4) resolved the issue for me.

image of command line output

lbarbosa
  • 2,042
  • 20
  • 23
Michael Nelles
  • 5,426
  • 8
  • 41
  • 57
3

There is no error in your output console.

It's just a warning saying that you are not running Firestore locally, which is totally normal since you run the command with --only functions and thus started only the cloud functions part (without firestore, rules, etc)

Simon
  • 6,025
  • 7
  • 46
  • 98
  • Thanks for the quick reply. So how can I start Firestore, rules etc. as well? Is it something I have to add to the function or a command i have to run in the terminal? – carloregian Apr 15 '20 at 09:56
  • you can start them by removing --only functions from your command – Simon Apr 15 '20 at 11:59
  • i've tried that but i get the same outcome, the function finishes as the promise is rejected. Any idea what could be wrong? – carloregian Apr 15 '20 at 13:18
  • It depends on the error, maybe a rule problem. You probably should ask an other question with the error ouput ;) – Simon Apr 15 '20 at 13:20
  • Then it's probably a rule problem – Simon Apr 15 '20 at 13:33
  • I realised that if you log the error to the console, it describes what the error is. Turns out it was an issue with my default credentials and I had to add the firebase admin sdk key to my project. It now works. Thanks for the help Simon :) – carloregian Apr 15 '20 at 13:47
  • You're welcome, you can accept my answer if it helped ;) – Simon Apr 15 '20 at 16:12