首页 > 解决方案 > 如何修复“无法获取未定义或空引用的属性'get'”

问题描述

我正在尝试创建应用程序。但是,我不断收到错误消息“无法在 ProfilePage.prototype.ngOnInit 处获取未定义或空引用的属性 'get'”。

我使用 ionic 4 作为我的框架。当我试图从我的主页导航到我的个人资料页面时发生此错误

HomePage.html:
<ion-header>
    <ion-toolbar>
      <ion-title>Home</ion-title>
      <ion-buttons slot="end">
        <ion-button routerLink="/profile">
          <ion-icon slot="icon-only" name="person"></ion-icon>
        </ion-button>
      </ion-buttons>
    </ion-toolbar>
</ion-header>
profile.page.ts
import { Component, OnInit } from '@angular/core';
import { AlertController } from '@ionic/angular';
import { AuthService } from '../../services/user/auth.service';
import { ProfileService } from '../../services/user/profile.service';
import { Router } from '@angular/router';

@Component({
  selector: 'app-profile',
  templateUrl: './profile.page.html',
  styleUrls: ['./profile.page.scss']
})
export class ProfilePage implements OnInit {
  public userProfile: any;
  public birthDate: Date;
  constructor(
    private alertCtrl: AlertController,
    private authService: AuthService,
    private profileService: ProfileService,
    private router: Router
  ) {}

  ngOnInit() {
    this.profileService
      .getUserProfile()
      .get()
      .then(userProfileSnapshot => {
        this.userProfile = userProfileSnapshot.data();
        this.birthDate = userProfileSnapshot.data().birthDate;
      });
  }
......
Homepage.ts

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.page.html',
  styleUrls: ['./home.page.scss'],
})
export class HomePage implements OnInit {

  constructor() { }

  ngOnInit() {}

}

上面的代码预计允许用户进入个人资料页面。但是结果是“无法在 ProfilePage.prototype.ngOnInit 处获取未定义或空引用的属性 'get'”

标签: angulartypescriptionic-frameworkionic4capacitor

解决方案


可能您this.profileService.getUserProfile()只是返回 null 或 undefined。使用 chrome 开发工具 Source 选项卡检查它(使用 ctrl (cmd) + p 搜索文件)


推荐阅读