如何在onwrite事件中获取firebase pushID?

时间:2018-06-07 04:15:49

标签: javascript node.js firebase google-cloud-datastore

如何获取推送ID?

我有一个看起来像这样的JSON:

purchaseTransaction: {
    xxxxxx: {
      amount: 1000,
      user: yyyyyyy
    }
}

当这个purchaseTransaction文档更新时,我想获得pushID(这是由firebase生成的)?

exports.addPurchaseTransactionToUser = functions.database.ref('/purchaseTransaction/{pushId}').onWrite((change, context) => {
  const snapshot = change.after;
  const val = snapshot.val();
  console.log('val',val);

  // How to get the pushID?

});

2 个答案:

答案 0 :(得分:1)

来自documentation on handling event data

exports.makeUppercase = functions.database.ref('/messages/{pushId}/original')
    .onCreate((snapshot, context) => {
      // Grab the current value of what was written to the Realtime Database.
      const original = snapshot.val();
      console.log('Uppercasing', context.params.pushId, original);

因此,在您的情况下,推送ID将以context.params.pushId提供。

答案 1 :(得分:1)

对于您的代码,请使用context.params.pushId