How do I remove a user by key or value?

删除回忆录丶 提交于 2019-12-11 08:38:12

问题


I have a structure in my Firebase database like this:

--users
    --v7BwBnUBqJMXCtoOHYE51jXCwhY2: uid
    --etc

The users 'table' gets populated when a user gets authenticated like this:

  loginFb() {
    this.af.auth.login({
      provider: AuthProviders.Facebook,
      method: AuthMethods.Popup,
    }).then(
        (user) => {
        this.userOnline(user.uid, 'uid');
        this.router.navigate(['/members']);
      }).catch(
        (err) => {
        this.error = err;
      })
  }

  userOnline(uid, value) {
    const users = this.af.database.object('/users');
    users.update({ [uid]: value });
  }

This gives ea user a uid in the user table thats generated by fb.

I want to remove the user when they logout. Researching this I have come across this answer.

I applied it as such:

  logout(userUID) {
    this.af.auth.logout();
    this.userOffline(userUID);
    this.router.navigateByUrl('/login');
  }

  userOffline(userUID) {

    const user = this.af.database.list('/', {
      query: {
        orderByChild: userUID,
        equalTo: 'uid'
      }
    });

    user.subscribe(snapshots=>{
      snapshots.forEach(snapshot => {
        console.log(snapshot);
        snapshot.ref.remove();
      });
    })

  }

The result of the console.log is:

Object {v7BwBnUBqJMXCtoOHYE51jXCwhY2: "uid", $key: "users"}

Alas I get this error:

Cannot read property 'remove' of undefined

I'm sure there is a better way to do this but I couldn't find anything. Any advice is appreciated.

EDIT

Thanks to @pengyy comment I made a minor amend to their suggestion.

user.subscribe(snapshots=>{
  snapshots.forEach(snapshot => {
    console.log(snapshot);
    this.af.database.list('/users').remove(snapshot.userUID)
  });
})

This works and removes the user but it does so straight away on page load not even when the function is called... the plot thickens


回答1:


remove item from table by

this.af.database.list('/users').remove(snapshot.$key)



回答2:


  userOffline(userUID) {
    this.af.database.list('/users/'+userUID).remove();
  }

So this works. So simple...

Thanks @pengyy I wouldn't have got there without your comments.



来源:https://stackoverflow.com/questions/42566692/how-do-i-remove-a-user-by-key-or-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!