How to write data to firestore emulator from web app?

219 views
Skip to first unread message

Nikita Zhidkov

unread,
Dec 5, 2021, 10:56:44 AM12/5/21
to Firebase Google Group
Hi How to write data to firestore emulator from web app? I init my app for emulator: firebaseConfig = {    apiKey: "project_api_key",    projectId: "local-id",   };   const app = firebase.initializeApp(firebaseConfig);   const db = getFirestore(app);   connectFirestoreEmulator(db, 'localhost', 8080); Add use await addDoc(collection(db, "emails"), {         email: email,       }); to write data. But unfortunatelly it doesn't write any information to firestore. Could you please clarify where I can find mistake? (If I write data through cloud functions all works fine)

Roger Hopkins

unread,
Dec 5, 2021, 1:48:25 PM12/5/21
to Firebase Google Group
Hi Nikita,

You've probably followed this but here's a link to the Firebase docs on connecting Firestore to the emulator: https://firebase.google.com/docs/emulator-suite/connect_firestore.

I had issues with data being written to the Firebase cloud database and not the emulator database.  

First, here's what I use in my code to initialize Firebase and setup the emulators.  These need to be in this order.
//-------->
const firebaseApp = firebase.initializeApp(firebaseConfig)
const firebaseStore = firebaseApp.firestore()

// *** I ran into issues before adding the following
firebaseStore.settings({
merge: true
})

// Use emulators
if (window.location.hostname === 'localhost') firebaseStore.useEmulator("localhost", 8080)
//-------<
Then you need to run the emulators on you local machine.  From a terminal command prompt run "firebase emulators:start".

Hope that helps.
Reply all
Reply to author
Forward
0 new messages