gpt4 book ai didi

javascript - Firebase 类型错误的云函数 - 无法读取属性

转载 作者:太空宇宙 更新时间:2023-11-04 03:05:10 24 4
gpt4 key购买 nike

我正在尝试编写一个云函数,每当有人使用我们的旧应用程序创建记录时,该函数都会创建一条记录(我们已经更改了 Firebase 后端架构,并希望慢慢迁移用户)。但是,我的日志中出现以下错误:

TypeError: Cannot read property 'update' of undefined
at exports.makeNewComment.functions.database.ref.onWrite.event (/user_code/index.js:14:92)
at /user_code/node_modules/firebase-functions/lib/cloud-functions.js:35:20
at process._tickDomainCallback (internal/process/next_tick.js:129:7)

这是有问题的脚本:

//required modules
var functions = require('firebase-functions');
const admin = require('firebase-admin');

// Listens for new comments added to /comments/ and adds it to /post-comments/

exports.makeNewComment = functions.database.ref('comments/{commentId}').onWrite(event => {
// Grab the current value of what was written to the Realtime Database.
const commentId = event.params.commentId;
const comment = event.data.val();
// You must return a Promise when performing asynchronous tasks inside a Functions such as
// writing to the Firebase Realtime Database.
//return event.data.ref.parent.child('post-comments').set(comment);
return functions.database.ref('post-comments/' + comment['postID'] + '/' + commentId).update(comment).then(url => {
return functions.database.ref('user-comments/' + comment['postedBy'] + '/' + commentId).update(comment);
});
});

//initialize
admin.initializeApp(functions.config().firebase);

谢谢!

最佳答案

您不能在函数中间使用functions.database.ref()来获取对数据库中某个位置的引用。这仅用于定义新的云函数。

如果您想要对数据库中某个位置的引用,可以使用 event.data.refevent.data.adminRef 来获取对事件触发位置的引用。然后,您可以使用它的 root 属性来重建对数据库中其他位置的新引用。或者您可以使用您的 admin 对象来构建新的引用。

查看一些 sample code 可能会有所帮助了解事情是如何运作的。

关于javascript - Firebase 类型错误的云函数 - 无法读取属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43080838/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com