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

How to get firebase pushID in onwrite event?

如何获取推送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?

});

来自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

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