1
votes

The problem

I'm using Firebase cloud functions with the emulator, and for some reason, the onUpdate trigger is not triggering, yet the onCreate function does trigger.

All code is TypeScript, and it's transpiled to JS to work on cloud functions.

// functions/src/music.ts

// this function runs
export const onMusicCreated = functions.firestore
    .document('music/{musicId}')
    .onCreate(async (snapshot) => {
        console.log('on create is running')
    })

// this function doesn't run
export const onMusicUpdated = functions.firestore
    .document('music/{musicId}')
    .onUpdate(async (change) => {
        console.log('on update is running')
    })

Both functions are async because in the final code,

On the front-end, when I run the add function on the front-end, the onCreate function fires.

const { id } = await firebase.firestore().collection('music').add({ title: 'hello world' })

The console log runs as expected and the emulator outputs this into the console:

i  functions: Beginning execution of "onMusicCreated"
i  functions: Finished "onMusicCreated" in ~1s

Yet when I update that same document, the onUpdate function doesn't run.

// "id" is the same id as above
await firebase.firestore().doc(`music/${id}`).update({ title: 'new title' })

Nothing happens. I can confirm that the document is actually updated when I look in the firestore emulator. Am I missing something obvious? The front-end code is simplified as compared to my actual use, but the functions code isn't simplified at all. And I can confirm that the firestore document is created and updated as I'd expect.

No error messages in the console or the logs.

Debug steps

  • I've checked to make sure the functions code was correctly transpiled to JS. I bothed looked at the code output, as well as updated the onCreate code multiple times to ensure that function updated
  • I hollowed out all my code inside the functions (as shown above), so I can confirm that the function itself isn't running
  • The onUpdate function technically accepts two parameters. Same results with both parameters.
  • I have not tried the functions in production, only with the emulator.

Related posts

Why doesn't firestore onWrite trigger get invoked on firebase cloud functions emulator?

I'm not using any forbidden characters in the document selector, or at least I'm not getting that error message.

Firebase Cloud Functions for Firestore are not triggering

Using functions 3.11.0, and the functions are async, hence they should implicitly return Promise. Results are the same when explicetely returning a value (e.g., return 0)

https://firebase.google.com/docs/functions/firestore-events#trigger_a_function_when_a_document_is_updated

That's the official docs. As far as I can tell, I'm doing what the docs say. I could just be missing something blindingly obvious, though.

Other details

  • macOS Big Sur 11.1 (20C69)
  • Firebase CLI 9.1.0

The emulator should be up-to-date

Any ideas? Thanks!

1

1 Answers

0
votes

Turns out I just forgot to import onMusicUpdated in the index.ts functions file. Hence the emulator never knew it existed!

Changed

// functions/src/index.ts

export { onMusicCreated } from './music'

to

// functions/src/index.ts

export { onMusicCreated, onMusicUpdated } from './music'