【发布时间】:2017-12-23 03:51:45
【问题描述】:
我正在使用 push 将评论表单数据写入 firebase 实时数据库。我有一个函数监视onWrite 事件以捕获发布的 cmets。
这是我的功能:
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);
exports.newComment = functions.database.ref('{postRef}')
.onWrite(event => {
console.log("Started!");
return theComment(event.data);
});
function theComment(snapshot) {
console.log("Running theComment");
var comment = snapshot._delta;
console.log(comment);
}
返回的树是:
{ '-L10TQfRr9IFhOsd1nJe':
{ md5Email: '93942e96f5acd83e2e047ad8fe03114d',
message: 'This is a test post.',
moderated: false,
name: 'demo',
postedAt: 1514000728416 }
}
我读到key 值将返回 v3 中的 ID,但是当我尝试记录 comment.key 时,它会记录 undefined。
有没有更好的方法来观察和访问新元素的编写?
【问题讨论】:
-
应该是
snapshot中的密钥的其他访问者 ...类似于snapshot.key或snapshot.ref.key
标签: javascript node.js firebase-realtime-database google-cloud-functions