如何通过键或值删除用户? [英] How do I remove a user by key or value?
问题描述
我的 Firebase 数据库中有这样的结构:
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 });
}
这会在 fb 生成的用户表中为 ea 用户提供一个 uid.
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.
我是这样应用的:
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();
});
})
}
console.log 的结果是:
The result of the console.log is:
Object {v7BwBnUBqJMXCtoOHYE51jXCwhY2: "uid", $key: "users"}
唉,我收到这个错误:
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.
编辑
感谢@pengyy 的评论,我对他们的建议做了一个小修改.
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
推荐答案
userOffline(userUID) {
this.af.database.list('/users/'+userUID).remove();
}
所以这是有效的.这么简单...
So this works. So simple...
谢谢@pengyy 如果没有您的评论,我就不会到达那里.
Thanks @pengyy I wouldn't have got there without your comments.
这篇关于如何通过键或值删除用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!