Search code examples
node.jsfirebase-realtime-databasegoogle-cloud-functionsfirebase-tools

Firebase Realtime Database Emulator: Can't read from database (snapshot is always null) even though onWrite function gets triggered


I have a very simple Cloud Function running in the emulator:

functions.region('europe-west1').database.ref('teams/{teamId}/members/{memberId}')
  .onWrite(async (change, context) => {
    const snapshot = await admin.database().ref('teams').once('value')
    console.log(26, snapshot.exists())
    console.log(snapshot.val())
})

When I change a member or add a new member in the emulator this function gets triggered (so 'teams' is 100% defined) however the snapshot is always empty and does not exist.

I am staring the emulator with the following command :

firebase emulators:start --import ./folder

The folder has a JSON file with my database. Anyone has any idea what is causing this? So frustrating, literally always see null in the console.


Solution

  • I had two different database instances in different regions. The emulator was listening for changes in database A but reading in database B. I had only emulated database A.