如何修复“无法获取未定义或空引用的 属性 'get'”
How to fix " Unable to get property 'get' of undefined or null reference "
我正在尝试创建应用程序。但是我一直收到错误提示 "Unable to get property 'get' of undefined or null reference at ProfilePage.prototype.ngOnInit".
我使用 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() {}
}
上面的代码应该允许用户转到个人资料页面。然而结果是 "Unable to get property 'get' of undefined or null reference at ProfilePage.prototype.ngOnInit"
可能您的 this.profileService.getUserProfile()
只是 returns null 或未定义。使用 chrome 开发工具源选项卡检查它(使用 ctrl (cmd) + p 搜索您的文件)
这样试试:
ngOnInit() {
if(this.profileService.getUserProfile() {
this.profileService
.getUserProfile()
.get()
.then(userProfileSnapshot => {
this.userProfile = userProfileSnapshot.data();
this.birthDate = userProfileSnapshot.data().birthDate;
});
}
}
我正在尝试创建应用程序。但是我一直收到错误提示 "Unable to get property 'get' of undefined or null reference at ProfilePage.prototype.ngOnInit".
我使用 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() {}
}
上面的代码应该允许用户转到个人资料页面。然而结果是 "Unable to get property 'get' of undefined or null reference at ProfilePage.prototype.ngOnInit"
可能您的 this.profileService.getUserProfile()
只是 returns null 或未定义。使用 chrome 开发工具源选项卡检查它(使用 ctrl (cmd) + p 搜索您的文件)
这样试试:
ngOnInit() {
if(this.profileService.getUserProfile() {
this.profileService
.getUserProfile()
.get()
.then(userProfileSnapshot => {
this.userProfile = userProfileSnapshot.data();
this.birthDate = userProfileSnapshot.data().birthDate;
});
}
}