[英]ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'length' of undefined in Angular 7 and rxjx
I have recently upgraded my angular 2 app to angular 7. One of the challenges that I had was the user.service.ts
where I have isAuthentiated
method is. 我最近将我的angular 2应用程序升级到了
user.service.ts
我遇到的挑战之一是使用isAuthentiated
方法的user.service.ts
。 Basically, this method is passed into the auth.guard.ts
so that I can direct users if their username is not on my list. 基本上,此方法被传递到
auth.guard.ts
这样,如果用户名不在我的列表中,我就可以引导用户。
User Service retrieves the users then isAuthenticated checks the users. 用户服务检索用户,然后isAuthenticated检查用户。 Auth guard is checking the user.
Auth Guard正在检查用户。 If the user is in the list, grant access, otherwise navigate them to a certain website.
如果用户在列表中,请授予访问权限,否则将其导航到某个网站。
user.service.ts
import { Injectable } from '@angular/core';
import { environment } from '../../environments/environment';
import { map, catchError, tap } from 'rxjs/operators';
import { HttpClient } from '@angular/common/http';
import { Observable, of } from 'rxjs';
@Injectable()
export class UserService {
baseUrl = environment.apiUrl;
constructor(private http: HttpClient) { }
private extractData(res: Response) {
const body = res;
return body || { };
}
... other methods
isAuthenticated(): Observable<boolean> {
return this.http.get(this.baseUrl + 'users').pipe(
map(this.extractData),
map(res => !!res.length)
);
}
}
And my auth.guard.ts
is: 我的
auth.guard.ts
是:
import { UserService } from './../_services/user.service';
import { Injectable } from '@angular/core';
import { CanActivate, RouterStateSnapshot, Router } from '@angular/router';
import { AlertifyService } from '../_services/alertify.service';
import { map } from 'rxjs/operators';
@Injectable()
export class AuthGuard implements CanActivate {
constructor(
private userService: UserService,
private alertifyService: AlertifyService,
private router: Router
) {}
canActivate(route, state: RouterStateSnapshot) {
return this.userService.isAuthenticated().pipe(map(user => {
if (!user) {
return true;
} else {
this.router.navigate(['/authentication/get-jhed']);
this.alertifyService.error('You need to be authenticated to access this area');
return false;
}
}));
}
}
This code returns the following error: 此代码返回以下错误:
ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'length' of undefined
错误错误:未捕获(承诺):TypeError:无法读取未定义的属性“ length”
I tried res[0].length
I didn't receive the error but nothing happened this time. 我尝试了
res[0].length
,但没有收到错误,但这次没有任何反应。
I am able to solve this issue using Http
as below: 我可以使用
Http
来解决此问题,如下所示:
isAuthenticated(): Observable<boolean> {
return this.http.get(this.baseUrl + 'users').pipe(
map(res => res.json()),
map(res => !!res.length)
);
}
Please note that I use Http
in here, NOT HttpClient
. 请注意,我在这里使用
Http
,而不是HttpClient
。
Any help would be appreciated! 任何帮助,将不胜感激!
HttpClient
by default indicates response body not a whole response object. 默认情况下,
HttpClient
指示响应主体不是整个响应对象。 If you migrate from Http
to HttpClient
at the same time - to be compatibile with the previous way - use options object to return whole response in callback. 如果您同时从
Http
迁移到HttpClient
(与以前的方法兼容),请使用options对象在回调中返回整个响应。
isAuthenticated(): Observable<boolean> {
return this.http.get(this.baseUrl + 'users', { observe: 'response' }).pipe(
map(this.extractData),
map(res => !!res.length)
);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.