如何通过键或值删除用户? [英] How do I remove a user by key or value?
问题描述
在我的Firebase数据库中有这样一个结构:
- 用户
--v7BwBnUBqJMXCtoOHYE51jXCwhY2:uid
--etc
当用户通过验证后, :
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});
$ b $ p
$ b这个给用户一个用户表中的uid, / p>
我想在注销时删除用户。研究这个我遇到了这个答案。
我这样应用它:
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'
}
});
$ b user.subscribe(snapshots => {
snapshots.forEach(snapshot => {
console.log(snapshot);
snapshot.ref.remove ();
});
})
}
$ b $ console.log的结果是:
对象{v7BwBnUBqJMXCtoOHYE51jXCwhY2:uid,$ key:users }
唉,我得到这个错误:
< pre $ 无法读取属性'删除'未定义
确定有一个更好的方法来做到这一点,但我什么都找不到。任何意见赞赏。
编辑
感谢@pengyy评论我做
user.subscribe(snapshots => {
snapshots.forEach(snapshot = > {
console.log(snapshot);
this.af.database.list('/ users')。remove(snapshot.userUID)
});
} )
这个工作和删除用户,但它在页面加载,被称为...情节变厚
userOffline(userUID){
this.af.database.list('/ users /'+ userUID).remove();
}
所以这个工作。这么简单...
感谢@pengyy如果没有您的意见,我将不会到达那里。
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
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.
这篇关于如何通过键或值删除用户?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!