Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use firestore emulator from client

I want to test locally my firebase functions. These functions make firestore queries.

So i start the emulator firebase emulators:start and in my client i use firebase.functions().useFunctionsEmulator('http://localhost:5001').

My functions work well when i call them in my client. I can read/write data inside the firestore emulator.

The problem :

I want to read the firestore emulator data directly inside my client, like :

firebase.firestore().collection('tests').get().then(tests => {
    console.log( tests.docs.map(test=>test.map) )
})

but i can't find how to set the firestore emulator inside my client.

here what i tried:

1) Firestore setting

firebase.firestore().settings({
    host:'http://localhost:8080',
    ssl:false
})

result :

i get @firebase/firestore: Firestore (6.3.5): Could not reach Cloud Firestore backend. Backend didn't respond within 10 seconds. inside my client console.

The http request returns 'Not found'

2) Set the emulator url inside my firebaseConfig

var firebaseConfig = {
    // ...
    databaseURL: "http://localhost:8080",
    // ...
}

firebase.initializeApp(firebaseConfig)

in this case, the remote server (https://firestore.googleapis.com..) is requested.

So i want to setup one of these two cases :

1) Using the remote firestore inside my functions emulators

OR

2) Using the local firestore emulator inside my client code.

Anyone has already done this ?

like image 759
Anthony JEAMME Avatar asked Aug 17 '19 14:08

Anthony JEAMME


1 Answers

Okay, this is trivial... In your firestore cient config you should have provided the host, not the origin for firestore (the protocol is set using the ssl parameter):

firebase.firestore().settings({
    host: 'localhost:8080',
    ssl: false
})

At least this solved it for me when I had the exact same error.

Just FYI to anyone who's reading this - if you run into problems with the firestore client, you can use debug level logging, just set firebase.firestore.setLogLevel('debug'). Had the OP done that, he might have noticed that firebase is accessing firestore at http://http://localhost:8080/...

like image 104
Avius Avatar answered Oct 03 '22 20:10

Avius