skip to Main Content

I am trying to migrate my firebase functions to V2.In firebase V1 I did:

exports.onOpenCourse = functions.firestore.
  document("courses/{courseId}") 
  .onUpdate(
    (change, context) => { 
      ...
      const id = context.params.courseId
      ...

So I was able to use the info in "context".As I understand, in V2 I now should do something like:

const { onDocumentCreated, document } = require("firebase-functions/v2/firestore");
onDocumentCreated( 
  { document: ""courses/{courseId}",},   
  (event) => {
    const snapshot = event.data;
    const info = snapshot.data();

How do I get context.params info in firebase functions V2?

The relevant example in the docs seems to still use V1: https://firebase.google.com/docs/firestore/extend-with-functions

I guess the core issue is that I am confused about exactly what these new functions are returning.
For example: According to the docs I found https.onCall returns any. Can I count on any to always contain params and data? https://firebase.google.com/docs/reference/functions/firebase-functions.https.md#httpsoncall

Any help would be appreciated!


Edit:

What I find confusing is that the examples in the docs only touch the base cases and often leave all the additional config and the exact data structures beind handed over unclear.

Here, the short example imports FirestoreEvent:
https://firebase.google.com/docs/functions/firestore-events?gen=2nd#writing-triggered_functions

From that I am guessing that the event that gets passed is of type FirestoreEvent as described here:
https://firebase.google.com/docs/reference/functions/2nd-gen/node/firebase-functions.firestore.firestoreevent

But here I dont see the data.data() function I was expecting. This is probably coming from the parent CloudEvent:
https://firebase.google.com/docs/reference/functions/2nd-gen/node/firebase-functions.cloudevent.md#cloudeventdata

…but here the documentation gets even sparser and data: T; does not tell me much.

So I guess I can count on database, document, location, namespace, params to be present in the return value from onDocumentCreated as well as data.data() which will give me the content of the Firestore document in question.

Is my understanding correct?

2

Answers


  1. The difference between functions from v1 and v2 is, that v2 needs to have req.data or req.context in order to retrieve either data or context. In v1 u’ve got directly parameter of data and context.

    Login or Signup to reply.
  2. You’re not looking at the best possible documentation. Use this reference instead, which lets you compare v1 and v2 easily.

    The parameters you want are in the event object provided to the function. There is no longer a "context" to use.

    const { onDocumentCreated, document } = require("firebase-functions/v2/firestore");
    onDocumentCreated( 
      { document: "courses/{courseId}" },   
      (event) => {
        const courseId = event.params.courseId;
    

    The second link to documentation you provided won’t be helpful at all because it’s for callable functions, which are not the same as Firestore functions.

    If you feel that some page of documentation is inaccurate or unhelpful, you should send feedback to Firebase using the "send feedback" button at the top of any page.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search