0
votes

I do appreciate this question has been answered in a few places. I'm new to Firebase cloud functions (and learning TS), so I'd just like to see the solution in my own context to fully understand the issue here.

My index.ts:

exports.OnPlanCreate = functions.database
.ref(`users/{uid}/plans/{key}`)
.onCreate((snapshot, context) => {
    const user: string = context.params.uid
    const fBaseKey: string = context.params.key
    // const plan: any = snapshot.val()
    console.log(`New plan created with key ${fBaseKey}for user ${user}`)

    // Update plan object key with Firebase generated DB key
    snapshot.ref.update({ key: fBaseKey })
    .then(() => {
        console.log('Plan key auto updated successfully!')
    })
    .catch((e) => {
        console.error(e)
    })
})

Gives the warning: "Function returned undefined, expected Promise or value"

I'd appreciate an explanation that helps me to understand the correct pattern to use in future :)

Thanks so much!

1

1 Answers

0
votes

This means you need to return from your function. So try this, it should work:

exports.OnPlanCreate = functions.database
.ref(`users/{uid}/plans/{key}`)
.onCreate((snapshot, context) => {
    const user: string = context.params.uid
    const fBaseKey: string = context.params.key
    // const plan: any = snapshot.val()
    console.log(`New plan created with key ${fBaseKey}for user ${user}`)

    // Update plan object key with Firebase generated DB key
    return snapshot.ref.update({ key: fBaseKey })
    .then(() => {
        console.log('Plan key auto updated successfully!')
    })
    .catch((e) => {
        console.error(e)
    })
})

If you want to learn about Typescript and Cloud Functions that's a great place to start: https://firebase.google.com/docs/functions/terminate-functions

Also if you need real-life examples there is a great GitHub repo owned by Firebase. Enjoy :)