Firebase的Cloud Functions中将对象从一个节点复制到另一个节点
我正在使用Firebase的云端函数,而且我似乎是一个非常基本的操作。
如果有人添加post,他会写入/posts/
。 我想要使用与初始文章中使用的相同的密钥将该post的一部分保存在不同的节点(称为public-posts
或private-posts
post)下。
我的代码看起来像这样
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')
- 从嵌套字典中创build一个数组
- “使用Node.JS的Firebase云function”中的“functions.config()不可用”
- 如何使用firebase云functionhttp触发器从firebase数据库获取数据
- FireStore – TypeError:k.setTimeout不是一个函数
- FCM – node.js中的定时器function
- 当每个函数结束时需要执行函数
- 从TypeScript Firebase Admin SDK初始化失败
- Nodejs,Cloud Firestore上传任务 – 身份validation错误:错误:套接字挂断
- 如何在firebase的云端函数中读取单个请求中多个用户ID的数据