[英]Angular ERROR TypeError: Cannot read property 'userName' of undefined
I'm new to Angular so bear with me.我是 Angular 的新用户,请多多包涵。
I wanted to get the username of the current user in my angular 10 application But when I try to load the dashboard, I get the following error.我想在我的 angular 10 应用程序中获取当前用户的用户名但是当我尝试加载仪表板时,出现以下错误。 The backend is developed with .net 5 web api.
后端用.net 5 web api开发。
ERROR TypeError: Cannot read property 'userName' of undefined
at ProfileComponent.loadMember (profile.component.ts:29)
at ProfileComponent.ngOnInit (profile.component.ts:23)
at callHook (core.js:3038)
at callHooks (core.js:3008)
at executeInitAndCheckHooks (core.js:2960)
at refreshView (core.js:7186)
at refreshEmbeddedViews (core.js:8279)
at refreshView (core.js:7195)
at refreshComponent (core.js:8325)
at refreshChildComponents (core.js:6964)
Here is My code in the Profile component where i try to fetch the user details这是我在配置文件组件中尝试获取用户详细信息的代码
export class ProfileComponent implements OnInit {
staff!: Staff;
user!: User;
constructor(private accountService: AuthenticationService, private staffService: StaffService) {
this.accountService.currentUser$.pipe(take(1)).subscribe(user => this.user = user);
}
ngOnInit(): void {
this.loadMember();
}
loadMember() {
this.staffService.getStaff(this.user.userName).subscribe(staff => {
this.staff = staff;
})
}
}
my StaffService code here is the code of the get staff service from the api我这里的 StaffService 代码是从 api 获取员工服务的代码
getStaff(userName: string) {
console.log(this.memberCache);
const member = [...this.memberCache.values()].reduce((arr, elem) => arr.concat(elem.result), [])
.find((member: Staff) => member.userName === userName);
if(member){
return of(member);
}
console.log(member);
return this.http.get<Staff>(this.baseUrl + 'AppUsers/staffByUserName/' + userName)
}
I cant understand why it is not working i try to conole out this.user.userName but it only returns undefined.我不明白为什么它不起作用我尝试控制 this.user.userName 但它只返回未定义。 is there a way to fix this?
有没有办法来解决这个问题?
you should do this instead so that you manage only one subscription.您应该改为这样做,以便您只管理一个订阅。
export class ProfileComponent implements OnInit {
staff: Staff;
user: User;
constructor(private accountService: AuthenticationService, private staffService: StaffService) {
}
ngOnInit(): void {
this.loadMember();
}
loadMember() {
this.accountService.currentUser$
.pipe(take(1), switchMap(user => this.staffService.getStaff(user.userName)))
.subscribe(staff => {
this.staff = staff;
})
}
}
Looks like the component is getting initialized before accountService has recieved a response, therefor by the time you call getStaff(this.user.username)
the user still hasn't been initiailized.看起来组件在 accountService 收到响应之前正在初始化,因此当您调用
getStaff(this.user.username)
时,用户仍未初始化。
Try delaying the call to this.loadMember()
until after the user has been initialized:尝试
this.loadMember()
的调用延迟到用户初始化之后:
ngOnInit(): void {
this.accountService.currentUser$.pipe(take(1)).subscribe(user =>{
this.user = user;
this.loadMember(this.user.username);
});
}
To avoid issues with values not being initialized, you can use rxjs
operators with Observables;为避免值未初始化的问题,您可以对 Observables 使用
rxjs
运算符;
export class ProfileComponent implements OnInit {
staff: Staff;
user: User;
// Define staff and user Observables. Use tap operator from 'rxjs/operators' to assign the values. Use mergeMap operator to call the getStaff function
user$: Observable<User> = this.accountService.currentUser$.pipe(
take(1),
tap(user => this.user = user)
);
staff$: Observable<Sfaff> = this.staff$.pipe(
mergeMap(({userName}) => this.staffService.getStaff(userName)),
tap(staff => this.staff = staff)
);
constructor(private accountService: AuthenticationService, private staffService: StaffService) {
}
ngOnInit(): void {
user$.subscribe();
staff$.subscribe();
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.