如何获取推送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?
});
答案 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
。