【发布时间】:2019-02-16 11:21:02
【问题描述】:
我是 Cloud Functions for Firebase 的新手,我正在尝试将它们与实时数据库一起使用。
我关注this documentation where it has a Youtube tutorial at the top of the page。
这是我在index.ts 中的代码:
import * as functions from 'firebase-functions';
export const onMessageCreate = functions.database
.ref('/party/{partyID}/messages/{message}')
.onCreate((snapshot, context) => {
const messageData = snapshot.val()
const text = addMore(messageData.text)
return snapshot.ref.update({ text: text })
})
function addMore(text: string): string {
return text.replace(/\bhello\b/g, 'hey')
}
但是,我的函数日志中出现以下错误:
TypeError: Cannot read property 'replace' of undefined
at addMore (/user_code/lib/index.js:12:16)
at exports.onMessageCreate.functions.database.ref.onCreate (/user_code/lib/index.js:8:18)
at cloudFunctionNewSignature (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:105:23)
at cloudFunction (/user_code/node_modules/firebase-functions/lib/cloud-functions.js:135:20)
at /var/tmp/worker/worker.js:733:24
at process._tickDomainCallback (internal/process/next_tick.js:135:7)
我确信正确的数据库结构如下所示:
是什么导致了这个错误?是否未安装某些“包”功能?
我正在运行 node v10.9.0 和 npm 6.2.0。
为 Doug 编辑:
谢谢 Doug,这是一个很棒的视频! ???我已经编辑了我的数据库,当我添加一条消息时它现在看起来像这样(以前我的函数尝试替换单词的尝试):
我还通过在末尾添加/text 更改了TypeScript 代码参考。我还使用String() 将messageData 转换为一个字符串,它消除了错误并显示了您所描述的单词“未定义”。正如你所说,我已将消息与孩子分开,但我仍然对为什么我没有收到价值以及为什么它添加额外的 text 孩子感到困惑。谢谢!
如果您有任何问题,请告诉我!
【问题讨论】:
标签: typescript firebase firebase-realtime-database google-cloud-functions