首页 文章

Firebase错误:函数返回未定义,预期的Promise或value

提问于
浏览
0

我很欣赏这个问题已经在一些地方得到了回答 . 我是Firebase Cloud 功能的新手(并且学习TS),所以我只想在我自己的上下文中看到解决方案,以完全理解这里的问题 .

我的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)
    })
})

发出警告:“函数返回未定义,预期的Promise或value”

我很感激一个解释,帮助我理解将来使用的正确模式:)

非常感谢!

1 回答

  • 0

    这意味着您需要从函数返回 . 所以尝试这个,它应该工作:

    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)
        })
    })
    

    如果您想了解Typescript和Cloud Functions,那么这是一个很好的起点:https://firebase.google.com/docs/functions/terminate-functions

    此外,如果你需要真实的例子,Firebase拥有一个很棒的GitHub repo . 请享用 :)

相关问题