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
So this works. So simple...
Thanks @pengyy I wouldn't have got there without your comments.
remove item from table by