firebase - Firebase 错误:函数返回未定义、预期的 Promise 或值
问题描述
我很感激这个问题已经在几个地方得到了回答。我是 Firebase 云功能(和学习 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 或值”
我很感激能帮助我理解将来使用的正确模式的解释:)
非常感谢!
解决方案
这意味着你需要从你的函数中返回。所以试试这个,它应该工作:
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存储库。享受 :)
推荐阅读
- tensorflow - 安装了多个版本的 CUDA 后,如何让 Tensorflow-GPU 在 Windows 上使用特定版本的 CUDA
- java - 在 Java 中使用 Gson 解析来自 HTTP 请求的 JSON 响应
- asp.net - 事件不会触发动态添加的控制
- ruby-on-rails - 我的 Rails 应用程序开始在一些随机查询中引发 ActiveRecord::ImmutableRelation
- reactjs - 什么时候是使用 React Router Replace(componentDidMount?) 的好时机
- python - 无法在 Python 中使用 FFMPEG 录制视频
- powershell - 通过 powershell 或其他方式发送 HTTP 数据包
- javascript - 如何使用 AngularJS 在表中动态加载 JSON 数据?
- python - 找不到在 IAM 中创建 S3 批处理作业所需的权限
- mysql - Mysql Server 不需要密码来访问和编辑数据库