简体   繁体   English

错误错误:未捕获(承诺):TypeError:无法读取Angular 7和rxjx中未定义的属性“ length”

[英]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.

相关问题 错误错误:未捕获(承诺中):TypeError:无法读取未定义的属性“CountryArr” - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'CountryArr' of undefined Angular 7 - 错误错误:未捕获(承诺):类型错误:无法读取未定义的属性“forEach” - Angular 7 - ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'forEach' of undefined 错误:未捕获(承诺):TypeError:无法读取未定义的属性“ customerName” - Error: Uncaught (in promise): TypeError: Cannot read property 'customerName' of undefined 错误 - 未捕获(承诺)类型错误:无法读取未定义的属性“数据” - Error - Uncaught (in promise) TypeError: Cannot read property 'data' of undefined 角度:错误错误:未捕获(承诺):类型错误:无法读取未定义的属性“设置” - Angular: ERROR Error: Uncaught (in promise): TypeError: Cannot read property 'set' of undefined 错误:未捕获(承诺):TypeError:无法读取未定义的属性“ title” - Error: Uncaught (in promise): TypeError: Cannot read property 'title' of undefined 错误::未捕获(承诺)类型错误:无法读取未定义的属性“内容” - Error:: Uncaught (in promise) TypeError: Cannot read property "content" of undefined 获取错误:“未捕获的类型错误:无法读取未定义的属性‘长度’” - Getting error: "Uncaught TypeError: Cannot read property 'length' of undefined" 错误 - &#39;&#39;Uncaught TypeError:无法读取属性&#39;length&#39;?未定义&#39;&#39; - Error - ''Uncaught TypeError: Cannot read property 'length"? of undefined'' 需要帮助-&gt;未捕获(承诺)TypeError:无法读取未定义的属性“ length” - Need help -> Uncaught (in promise) TypeError: Cannot read property 'length' of undefined
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM