-1

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);

SITUATION:

I call it at the end of my app.js

Nothing happens.

Nothing gets printed to the console after "1" and "2".


EDIT:

I cannot call .remove() on a snapshot.

Also, from the docs:

"To delete a user, the user must have signed in recently. See Re-authenticate a user."

How can I delete a user then ?


REFERENCE:

Delete firebase data older than 2 hours

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

Community
  • 1
  • 1
Coder1000
  • 4,071
  • 9
  • 35
  • 84
  • If you set up a minimal jsbin that reproduces the problem I'll have a look. – Frank van Puffelen Jan 23 '17 at 17:16
  • @FrankvanPuffelen How can I do that when admin credentials are needed ? EDIT: Also, I tried with another firebase func like once("value") and that one worked. I do not know what's wrong. – Coder1000 Jan 23 '17 at 17:27
  • @FrankvanPuffelen https://jsbin.com/yijalokalu/edit?html,js,output – Coder1000 Jan 23 '17 at 17:29
  • Putting the code from your question in a jsbin does not help. You will have to spend the effort to reproduce the problem, which seems to be attaching a dynamic query. If you can't reproduce it in a jsbin, I'm afraid I won't be able to help. – Frank van Puffelen Jan 23 '17 at 19:16
  • @FrankvanPuffelen I found the issue. Thanks for pointing me in the right direction :) – Coder1000 Jan 23 '17 at 20:19

1 Answers1

0

There is no delete method on the snapshot. Instead, you should just call remove on the snapshot's 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);
    });
});

To delete the user account - in addition to the user data you have stored - you also need to call the deleteUser method (as you are running this on Node using 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);
    });
});
Coder1000
  • 4,071
  • 9
  • 35
  • 84
cartant
  • 57,105
  • 17
  • 163
  • 197
  • I still need to delete the user in the authentication system though. I can't call that method on the snaphot, so how can I call that method ? – Coder1000 Jan 17 '17 at 12:15
  • You call it on a `User` instance. There's no point calling it on the snapshot if it's a user method. – cartant Jan 17 '17 at 12:25
  • I know, but how do I do it then ? – Coder1000 Jan 17 '17 at 13:36
  • Fixed an error in the answer re: `snapshot.ref` - it's a property, not a method - but it's not possible to answer your question about deleting the user's account without more information. Are you running this on Node - using `firebase-admin`? And where in the snapshot's data is the account's `uid`? Without it, you cannot delete the account. – cartant Jan 17 '17 at 18:54
  • I updated my question. I am using firebase-admin and the uid is the key of the snapshot. – Coder1000 Jan 17 '17 at 19:06
  • Updated the answer with `deleteUser`. – cartant Jan 17 '17 at 19:12
  • This is extremely strange. Only 1 and 2 get printed to the console. But not 3. The firebase function is not triggered. – Coder1000 Jan 17 '17 at 19:23
  • TypeError: firebase.auth(...).deleteUser is not a function – Coder1000 Jan 23 '17 at 19:59
  • Please modify your answer: it should be admin.auth().deleteUser(...) so I can accept it. – Coder1000 Jan 23 '17 at 20:19