如何在Firebase中自动删除用户?

时间:2017-01-17 12:07:15

标签: javascript node.js firebase firebase-realtime-database

CODE:

app.js

setInterval(function() {

    console.log("1");
    var pendingRef = admin.database().ref("pending");
    var now = Date.now();
    var cutoff = now - 1 * 60 * 60 * 1000;
    var old = pendingRef.orderByChild('timestamp').endAt(cutoff).limitToLast(1);
    console.log("2");
    var listener = old.on('child_added', function(snapshot) {
        console.log("3");
        console.log("A VALUE:"+snapshot.val());
        snapshot.delete().then(function() {
            snapshot.ref().remove();
        }, function(error) {
            console.log("USER WAS NOT DELETED:"+ snapshot.val().key);
        });
    });
}, 1000 * 10);

状况:

我在app.js

的末尾打电话给我

什么都没发生。

“1”和“2”之后没有任何内容打印到控制台。

修改

我无法在.remove()上致电snapshot

另外,来自文档:

“要删除用户,用户必须最近已登录。请参阅重新验证用户。”

如何删除用户?

参考:

Delete firebase data older than 2 hours

https://firebase.google.com/docs/auth/web/manage-users#delete_a_user

1 个答案:

答案 0 :(得分:0)

快照上有no delete method。相反,您应该在快照的remove

上致电ref
var listener = old.on('child_added', function (snapshot) {
  console.log("EXECUTING...");
  console.log("A VALUE:" + snapshot.val());
  snapshot.ref
    .remove()
    .catch(function (error) {
      console.log("USER WAS NOT DELETED:" + snapshot.key);
    });
});

要删除用户帐户 - 除了您存储的用户数据外 - 您还需要调用deleteUser方法(因为您使用firebase-admin在节点上运行此方法):

var listener = old.on('child_added', function (snapshot) {
  console.log("EXECUTING...");
  console.log("A VALUE:" + snapshot.val());
  snapshot.ref
    .remove()
    .then(function () {
      return admin.auth().deleteUser(snapshot.key);
    })
    .catch(function (error) {
      console.log("USER WAS NOT DELETED:" + snapshot.key);
    });
});