从Firebase数据库中删除值

时间:2018-06-05 20:27:13

标签: javascript firebase firebase-realtime-database

我使用OneSignal从云端功能发送推送通知。 OneSignal响应还向我发送了一个player ids的数组,但该数组无法正常工作。我想从数据库中删除它们。有没有办法在不知道userKey的情况下删除它们?

数据库:

playerIds
    userKey
      player_id1: true,
      plaher_id2: true

云功能:

      let errors = response.errors.invalid_player_ids;

      let playerIdsRef = admin.database().ref('/playerIds/');

      for (let error of errors) {
        console.log(error)
        playerIdsRef.orderByChild(error).equalTo(true).on("value", function (snapshot) {
          console.log(snapshot);
          console.log(snapshot.val()); //this returns null
          snapshot.delete(); // this returns snapshot.delete is not a function

        });
      }

1 个答案:

答案 0 :(得分:0)

这就是我解决问题的方法:

    if (response.errors.invalid_player_ids.length > 0) {
      const errors = response.errors.invalid_player_ids;
      const playerIdsRef = admin.database().ref('/playerIds/');

      for (let error of errors) {
        playerIdsRef.orderByChild(error).equalTo(true).once("value", (snapshot) {
          const snapshotValue = snapshot.val();
          const key = Object.keys(snapshotValue)[0];
          //or
          //const key = Object.keys(snapshot.val())[0];
          playerIdsRef.child(key).child(error).set(null);
        });
      }
    }