0
push
を使用してfirebaseリアルタイムデータベースにコメントフォームデータを書きます。私はonWrite
イベントが投稿されたコメントをキャプチャするのを見ている機能があります。ここでFirebase固有のIDにアクセス
は私の機能です:
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'のようなものです – charlietfl