0
votes

I've been following a tutorial (https://www.youtube.com/watch?v=9kRgVxULbag&t=1108s) and I'm trying to get .onCreate to work, but nothing's happening.

The code I'm trying to replicate is at around 19:30 in the video, and here's what I've got:

exports.createinvite = functions.firestore
    .document('referrals/{referralid}')
    .onCreate(event => {
    console.log('asfasdfasdf')

    const docid = event.params.referralid;
    const code = event.data.data().thing;
    const referralref = admin.filestore().collection('referrals').doc(docid)

    return referralref.update({message: `asfasfasdfa`})
});

Any help is appreciated :)

2
How are you triggering the function? Also: is your function getting invoked (you can see this in the logging output), or not even that? - Frank van Puffelen
For now I’m just manually adding documents to the collection. And sorry, what do you mean by logging output? I’m new to all of this haha - Speedy Turtlez
The Cloud Functions Logs panel in the Firebase console shows the invocations and output of recent Cloud Functions. - Frank van Puffelen
It says the function is being run, and the error is: TypeError: Cannot read property ‘referralid’ of undefined at exports.createinvite.functions.firestore.document.onCreate.event - Speedy Turtlez

2 Answers

0
votes

You are following a very old tutorial, and I suggest you stop following it and learn from something newer, such as the official Firebase videos on YouTube. The APIs have changed.

Please also follow the documentation for Firestore triggers to learn what an onCreate function looks like today. They take two parameters, a snapshot and a context:

exports.createinvite = functions.firestore
    .document('referrals/{referralid}')
    .onCreate((snapshot, context) => { ... })

The wildcards from the pattern are available in context.params. You will use context.params.referralid to get the value of the wildcard.

0
votes

If you look at the documentation for Firestore event triggers, you'll see that onCreate is called with two parameters: snapshot and context. The params property exists on the context/second parameter, and not on the first.

So:

exports.createinvite = functions.firestore
    .document('referrals/{referralid}')
    .onCreate((snapshot, context) => {
    console.log('asfasdfasdf')

    const docid = context.params.referralid;
    const code = snapshot.data().thing;
    ...

I highly recommend checking out the documentation if something from a tutorial doesn't work, as the product may have been updated since the tutorial was created.