Firebase的Cloud Functions中将对象从一个节点复制到另一个节点

我正在使用Firebase的云端函数,而且我似乎是一个非常基本的操作。

如果有人添加post,他会写入/posts/ 。 我想要使​​用与初始文章中使用的相同的密钥将该post的一部分保存在不同的节点(称为public-postsprivate-postspost)下。

我的代码看起来像这样

 const functions = require('firebase-functions'); exports.copyPost = functions.database .ref('/posts/{pushId}') .onWrite(event => { const post = event.data.val(); const smallPost = (({ name, descr }) => ({ name, descr }))(post); if (post.isPublic) { return functions.database.ref('/public-posts/' + event.params.pushId) .set(smallPost); } else { return functions.database.ref('/private-posts/' + event.params.pushId) .set(smallPost); } }) 

我得到的错误信息是: functions.database.ref(…).set不是一个函数

我究竟做错了什么?

如果要在数据库触发器中更改数据库,则必须使用Admin SDK,或使用事件中提供的引用来查找对相关节点的引用。 (您不能使用functions.database来查找引用 – 用于注册触发器)。

最简单的事情可能是使用event.data.ref ( doc )find你想写的位置的引用:

 const root = event.data.ref.root const pubPost = root.child('public-posts')