【问题标题】:Object is exist with value, but when access the property returned undefined对象是有值存在的,但是当访问属性时返回 undefined
【发布时间】:2020-02-25 11:50:42
【问题描述】:

我发现奇怪的东西。我有 AuthService 可以保存我的应用程序的身份验证需求,包括身份验证令牌。

@IonicPage()
@Component({
  selector: 'page-login',
  templateUrl: 'login.html',
})
export class LoginPage {

  constructor(public navCtrl: NavController, public navParams: NavParams, public modalCtrl:ModalController,public auth: AuthService) {

  }

  ionViewDidLoad() {
    console.log(this.auth)
    console.log(this.auth.loggedIn)
    if(this.auth.loggedIn){
      console.log(this.auth);
      this.navCtrl.push("TabsPage");
    }    
  }
}

当我打电话时

console.log(this.auth)

它返回了authentication 但是当我打电话时

console.log(this.auth.loggedIn)

返回空值

这是我的 auth.service.ts

import { Injectable, NgZone, Component } from '@angular/core';
import { Storage } from '@ionic/storage';

// Import AUTH_CONFIG, Auth0Cordova, and auth0.js
import { AUTH_CONFIG } from './auth.config';
import Auth0Cordova from '@auth0/cordova';
import * as auth0 from 'auth0-js';

@Injectable()
export class AuthService {
  Auth0 = new auth0.WebAuth(AUTH_CONFIG);
  Client = new Auth0Cordova(AUTH_CONFIG);
  accessToken: string;
  user: any;
  loggedIn: boolean;
  loading = true;

  constructor(
    public zone: NgZone,
    private storage: Storage
  ) {
    this.storage.get('profile').then(user => this.user = user);
    this.storage.get('access_token').then(token => this.accessToken = token);
    this.storage.get('expires_at').then(exp => {
      this.loggedIn = Date.now() < JSON.parse(exp);
      this.loading = false;
    });

  }

  login() {
    this.loading = true;
    const options = {
      scope: 'openid profile offline_access'
    };
    // Authorize login request with Auth0: open login page and get auth results
    this.Client.authorize(options, (err, authResult) => {
      if (err) {
        throw err;
      }
      // Set access token
      this.storage.set('access_token', authResult.accessToken);
      this.accessToken = authResult.accessToken;
      // Set access token expiration
      const expiresAt = JSON.stringify((authResult.expiresIn * 1000) + new Date().getTime());
      this.storage.set('expires_at', expiresAt);
      // Set logged in
      this.loading = false;
      this.loggedIn = true;
      // Fetch user's profile info
      this.Auth0.client.userInfo(this.accessToken, (err, profile) => {
        if (err) {
          throw err;
        }
        this.storage.set('profile', profile).then(val =>
          this.zone.run(() => this.user = profile)
        );
      });
    });
  }

  logout() {
    this.storage.remove('profile');
    this.storage.remove('access_token');
    this.storage.remove('expires_at');
    this.accessToken = null;
    this.user = null;
    this.loggedIn = false;
  }

  isLoggedIn() :boolean{
    return this.loggedIn;
  }
}

我正在使用 ionic3 和 auth0 身份验证,以前我认为我没有在我的财产上使用公共标识符是我的错。但是当我将属性更改为公共,或创建返回属性的 getter 方法时,它仍然无法正常工作。

【问题讨论】:

    标签: javascript angular typescript ionic3


    【解决方案1】:

    这是由于 chrome 控制台评估对象时造成的。如果您在控制台中打开该对象,您会看到一个蓝色的小信息图标。这会说:

    刚刚评估了值

    基本上发生的情况是对象内容在您记录它的时间和您在控制台中打开它的时间之间发生了变化。

    登录动作是异步的,这意味着在调用ionViewDidLoad 之后,auth 对象上的loggedIn 属性将被设置。也许一件好事是将身份验证设置在 APP_INITIALIZER 提供程序中,或者在您的身份验证上设置一些 Observable,您可以在其上监听身份验证更改

    【讨论】:

    • 如果我在 app_initilzer 中设置了身份验证。我还需要可观察的吗?
    • @rezaridwansyah 我认为您会这样做,例如当身份验证令牌过期且用户仍处于登录状态时
    【解决方案2】:

    1.您在分配之前对 this.loggedIn 进行了整理,因此它是未定义的。
    在您的情况下,在登录后写入 console.log(this.auth.loggedIn) 。请检查那个场景。

    2.现在,为loggedIn变量分配一些值然后打印它会打印一个值

    loggedIn: boolean; => loggedIn: boolean=false; 然后打印一个它会起作用的值

    在其他组件中

     ngOnInit() {
        console.log(this.auth.loggedIn)
      }
    

    【讨论】:

    • 在 auth.service.ts 我使用 isLoggedIn() :boolean{ return this.loggedIn; } 并且仍然返回 undefined
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多