[英]Angularfire2 - Get the current user UID and filter list observable
How do I get the current user UID and use it to filter a list observable? 如何获取当前用户的UID并使用它来过滤可观察的列表?
I tried to use const currentUser = this.auth.getAuth()
. 我尝试使用
const currentUser = this.auth.getAuth()
。 It returns null and gives me a console.log warning to use const currentUser = this.auth.subscribe();
它返回null并给我一个console.log警告,以使用
const currentUser = this.auth.subscribe();
When I console.log the value of currentUser, I get currentuser: [object Object]
back. 当我console.log时currentUser的值时,我得到
currentuser: [object Object]
。
This is the complete function I made 这是我做的完整功能
showUserProfile() {
//const currentUser = this.auth.getAuth();
const currentUser = this.auth.subscribe();
console.log('currentuser: ' + currentUser);
return this.af.database.list('/', {
query: {
orderByChild: 'useruid',
equalTo: 'miIMjfVx6TgC9zo6jE0yn0I3JbC2' // currentUser.uid
}
});
}
second attempt with help from Alexander Ciesielski 在亚历山大·西耶尔斯基(Alexander Ciesielski)的帮助下进行第二次尝试
Alexander suggested this, but unfortunately it is giving to following error: Type 'void' is not assignable to type 'FirebaseListObservable<UserProfile[]>'.
亚历山大(Alexander)提出了这个建议,但不幸的是,它导致了以下错误:
Type 'void' is not assignable to type 'FirebaseListObservable<UserProfile[]>'.
showUserProfile() {
this.auth.subscribe(authData => {
console.log(authData);
let uid = authData.uid;
return this.af.database.list('/', {
query: {
orderByChild: 'useruid',
equalTo: uid // currentUser.uid
}
});
});
}
I tried the following next, sadly console.log('the output: ' + output);
我接下来尝试了以下操作,可悲的是
console.log('the output: ' + output);
returns the output: undefined
返回
the output: undefined
showUserProfile() {
let output;
this.auth.subscribe(authData => {
const currentUser = authData.uid;
console.log('Current User ID: ' + currentUser);
output = this.af.database.list('/', {
query: {
orderByChild: 'useruid',
equalTo: currentUser
}
});
})
console.log('the output: ' + output);
return output;
}
More information 更多信息
The showUserProfile method is called in a different component. 在不同的组件中调用showUserProfile方法。
userProfiles: FirebaseListObservable<UserProfile[]>
ngOnInit() {
this.userProfiles = this.authService.showUserProfile();
console.log('userprofiles: ' + this.userProfiles);
}
.subscribe()
is asynchronous. .subscribe()
是异步的。
That means you need to pass a callback function
as a parameter to .subscribe()
. 这意味着您需要将
callback function
作为参数传递给.subscribe()
。
When subscribe()
resolves it will call the callback function
with the actual data, in this case the auth object. 当
subscribe()
解析后,它将使用实际数据(在这种情况下为auth对象subscribe()
调用callback function
。
userProfiles: FirebaseListObservable<UserProfile[]>
...
this.auth.subscribe(authData => {
console.log(authData);
let uid = authData.uid;
this.userProfiles = this.af.database.list('/', {
query: {
orderByChild: 'useruid',
equalTo: uid // currentUser.uid
}
});
});
The userProfiles
list has to be set from inside the callback. 必须从回调内部设置
userProfiles
列表。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.