如何在Cloud Function中获得此错误值?还有如何更改pushId

时间:2019-02-20 16:55:22

标签: firebase firebase-realtime-database google-cloud-functions

enter image description here

exports.myFunction = functions.database.ref('/isAllowed/{pushId}')
.onWrite(event => {

    });
});

1 个答案:

答案 0 :(得分:0)

您在问题中使用的语法是与1.0.0之前的版本相对应的Firebase SDK for Cloud Functions。您应该使用最新版本,并使用其他语法,请参见https://firebase.google.com/docs/functions/beta-v1-diff

然后对于> = 1.0的版本,您将执行以下操作:

exports.myFunction = functions.database
  .ref('/isAllowed/{pushId}')
  .onWrite((change, context) => {
    // const beforeData = change.before.val();  data before the write
    const afterData = change.after.val(); // data after the write
    console.log(afterData);  // -> you get false
    return change.after.ref.set(true);  // -> you update to e.g. true
  });

如上述文档中所述

  

对于onWrite和onUpdate事件,data参数具有before和   后田。这些都是具有相同方法的DataSnapshot   在admin.database.DataSnapshot中可用。

因此,在上面的代码中,我们使用此DataSnapshot的ref属性通过set()方法(也可以使用update())对其进行更新。