question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Firebase Emulator error when writing/updating document inside a Firestore trigger, when using snapshot.ref.set()

See original GitHub issue

[REQUIRED] Environment info

firebase-tools: 7.4.0

Platform: Windows

[REQUIRED] Test case

Since auth is not available in the context for Firestore triggers, I send along some user data in the snapshot to log the activity and use the user token to create some extra steps inside the onCreate function. I extract the userdata from the snapshot and the want to remove the userdata using the snapshot.ref.set(snapShotWithoutUserData)

[REQUIRED] Steps to reproduce

export const onOrganizerCreate = functions.firestore.document("organizers/{organizerId}")
.onCreate((snapshot, context) => {
  return snapshot.ref.set({
    test: "testing"
  }, { merge: true });
})

I have tried ref.set(), ref.update() with or without the merge parameter, same result

[REQUIRED] Expected behavior

Expect it to run without errors as it does when i deploy it.

[REQUIRED] Actual behavior

The emulator/trigger crashes, giving this error

If i deploy the same code and run it without the emulator, it works just fine. Or if i use the firebase-admin to do the same task by building the path using the context params, it also works

! Non-default “firebase-admin” instance created!

  • This instance will not be mocked and will access production resources. ! Google API requested!
  • URL: “https://oauth2.googleapis.com/token
  • Be careful, this may be a production service. ! functions: Error: Getting metadata from plugin failed with error: invalid_grant at Http2CallStream.call.on (C:\Development<projectId>\functions\node_modules@grpc\grpc-js\build\src\client.js:96:45) at Http2CallStream.emit (events.js:194:15) at Http2CallStream.EventEmitter.emit (domain.js:441:20) at process.nextTick (C:\Development<projectId>\functions\node_modules@grpc\grpc-js\build\src\call-stream.js:71:22) at process._tickCallback (internal/process/next_tick.js:61:11) ! Your function was killed because it raised an unhandled error. (node:13324) UnhandledPromiseRejectionWarning: Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client at ServerResponse.setHeader (_http_outgoing.js:470:11) at ServerResponse.header (C:\Users\ralcar\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\express\lib\response.js:771:10) at ServerResponse.send (C:\Users\ralcar\AppData\Roaming\npm\node_modules\firebase-tools\node_modules\expressginated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:12 (8 by maintainers)

github_iconTop GitHub Comments

2reactions
samtsterncommented, Sep 30, 2019

@ralcar thanks!

FYI initializeApp(config().firebase) is outdated, you should just use initializeApp() … that doesn’t change the bug here but it’s worth noting.

1reaction
samtsterncommented, Oct 15, 2019

The fix for this issue has been released in version 7.6.0

Read more comments on GitHub >

github_iconTop Results From Across the Web

firebase - Firestore trigger not working when run in emulator
A new collection is automatically created when you create a new document using the set() method. There is no separate method for creating ......
Read more >
Connect your app to the Cloud Firestore Emulator - Firebase
The Firebase Local Emulator Suite emulates products for a single Firebase project. To select the project to use, before you start the emulators,...
Read more >
Getting Started With Cloud Firestore for iOS - Code
Mobile coders have been taking advantage of Google's Mobile Backend as a Service (MBaaS) platform Firebase Realtime Database for many years, ...
Read more >
Flutter Tutorial: How to use the Firebase Local Emulator with ...
in turn, this triggers a Cloud Function that recalculates all the totals and writes them to a separate Firestore document. finally, the client ......
Read more >
Testing security rules | Firestore - Google Cloud
The Firestore emulator does not work with the normal Firebase Authentication flow. Instead, in the Firebase Test SDK, we have provided the initializeTestApp()...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found