如何解决 Cannot read 属性 of "something" undefined
how to resolve cannot read property of "something" undefined
我正在尝试对 sqlite 数据库进行 CRUD 操作,创建 table 工作但更新功能出现问题。
我尝试更改使用 table 的 id 的路由路径并转到更新页面。
matches.ts
export class MatchesPage implements OnInit {
matches: Match[] = [];
match = {};
//myDate: String = new Date().toISOString();
constructor(private db: SqlitehelperService, private router: Router) {}
ngOnInit() {
this.db.getDatabaseState().subscribe(rdy => {
if (rdy) {
this.db.getMat().subscribe(match => {
this.matches = match;
});
}
});
}
addMatch() {
this.db
.addMatch(
this.match["id"],
this.match["btname"],
this.match["bwname"],
this.match["ocphone"],
this.match["overs"],
this.match["date"]
)
.then(_ => {
console.log(this.match);
this.match = {};
});
}
}
match.html
<ion-header>
<ion-toolbar>
<ion-title>Start Match</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-input *ngIf="" [(ngModel)]="match.id"></ion-input>
<ion-item>
<ion-label position="stacked">Batting Team Name</ion-label>
<ion-input [(ngModel)]="match.btname" placeholder="Batting Team Name"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Bowling Team Name</ion-label>
<ion-input [(ngModel)]="match.bwname" placeholder="Bowling Team Name"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Other Captain Phone</ion-label>
<ion-input [(ngModel)]="match.ocphone" placeholder="03XXXXXXXXX"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Overs</ion-label>
<ion-input [(ngModel)]="match.overs" placeholder="20 | 50"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Date</ion-label>
<ion-datetime displayFormat="YYYY-MM-DD THH:mmZ " [(ngModel)]="match.date"></ion-datetime>
</ion-item>
<ion-button expand="block" (click)="addMatch()">Add Match Info</ion-button>
**here i am navigating to the next page by router link and sending the below added match by clicking on particular item.**
<hr>
<ion-list>
<ion-item button *ngFor="let item of matches" [routerLink]="[ '/', 'matches', item.matchID ]">
<ion-label>
<h2>{{ item.battingTeamName }}</h2>
<br>
<p>{{ item.bowlingTeamName }}</p>
<br>
<p>{{item.otherCaptainPhone}}</p>
<br>
<p>{{item.overs}}</p>
<br>
<p>{{item.createdAt}}</p>
<br>
</ion-label>
</ion-item>
</ion-list>
</ion-content>
**singleMatchpagesmatch.ts**
通过路径 matches/:matchID.
接收 id
export class SmatchPage implements OnInit {
match: Match = null;
constructor(
private route: ActivatedRoute,
private db: SqlitehelperService,
private router: Router,
private toast: ToastController
) {}
ngOnInit() {
this.route.paramMap.subscribe(para => {
let mtid = para.get("id");
this.db.getMatch(mtid).then(data => {
this.match = data;
});
});
}
更新函数为:
updateMatch() {
this.db.updateMatch(this.match).then(async res => {
let toast = await this.toast.create({
message: "Match info updated",
duration: 3000
});
toast.present();
});
}
iam getting this error "cannot read property matchID of undefined"
请帮助我,我检查了所有内容,但无法弄清楚我的代码中有什么问题。
尝试使用安全导航运算符如下
<ion-item button *ngFor="let item of matches" [routerLink]="[ '/', 'matches', item?.matchID ]">
我想它会解决问题
<ion-list *ngIf="matches.length > 0">
<ion-item button *ngFor="let item of matches" [routerLink]="[ '/', 'matches', item.matchID ]">
我正在尝试对 sqlite 数据库进行 CRUD 操作,创建 table 工作但更新功能出现问题。
我尝试更改使用 table 的 id 的路由路径并转到更新页面。
matches.ts
export class MatchesPage implements OnInit {
matches: Match[] = [];
match = {};
//myDate: String = new Date().toISOString();
constructor(private db: SqlitehelperService, private router: Router) {}
ngOnInit() {
this.db.getDatabaseState().subscribe(rdy => {
if (rdy) {
this.db.getMat().subscribe(match => {
this.matches = match;
});
}
});
}
addMatch() {
this.db
.addMatch(
this.match["id"],
this.match["btname"],
this.match["bwname"],
this.match["ocphone"],
this.match["overs"],
this.match["date"]
)
.then(_ => {
console.log(this.match);
this.match = {};
});
}
}
match.html
<ion-header>
<ion-toolbar>
<ion-title>Start Match</ion-title>
</ion-toolbar>
</ion-header>
<ion-content>
<ion-input *ngIf="" [(ngModel)]="match.id"></ion-input>
<ion-item>
<ion-label position="stacked">Batting Team Name</ion-label>
<ion-input [(ngModel)]="match.btname" placeholder="Batting Team Name"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Bowling Team Name</ion-label>
<ion-input [(ngModel)]="match.bwname" placeholder="Bowling Team Name"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Other Captain Phone</ion-label>
<ion-input [(ngModel)]="match.ocphone" placeholder="03XXXXXXXXX"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Overs</ion-label>
<ion-input [(ngModel)]="match.overs" placeholder="20 | 50"></ion-input>
</ion-item>
<ion-item>
<ion-label position="stacked">Date</ion-label>
<ion-datetime displayFormat="YYYY-MM-DD THH:mmZ " [(ngModel)]="match.date"></ion-datetime>
</ion-item>
<ion-button expand="block" (click)="addMatch()">Add Match Info</ion-button>
**here i am navigating to the next page by router link and sending the below added match by clicking on particular item.**
<hr>
<ion-list>
<ion-item button *ngFor="let item of matches" [routerLink]="[ '/', 'matches', item.matchID ]">
<ion-label>
<h2>{{ item.battingTeamName }}</h2>
<br>
<p>{{ item.bowlingTeamName }}</p>
<br>
<p>{{item.otherCaptainPhone}}</p>
<br>
<p>{{item.overs}}</p>
<br>
<p>{{item.createdAt}}</p>
<br>
</ion-label>
</ion-item>
</ion-list>
</ion-content>
**singleMatchpagesmatch.ts** 通过路径 matches/:matchID.
接收 id export class SmatchPage implements OnInit {
match: Match = null;
constructor(
private route: ActivatedRoute,
private db: SqlitehelperService,
private router: Router,
private toast: ToastController
) {}
ngOnInit() {
this.route.paramMap.subscribe(para => {
let mtid = para.get("id");
this.db.getMatch(mtid).then(data => {
this.match = data;
});
});
}
更新函数为:
updateMatch() {
this.db.updateMatch(this.match).then(async res => {
let toast = await this.toast.create({
message: "Match info updated",
duration: 3000
});
toast.present();
});
}
iam getting this error "cannot read property matchID of undefined"
请帮助我,我检查了所有内容,但无法弄清楚我的代码中有什么问题。
尝试使用安全导航运算符如下
<ion-item button *ngFor="let item of matches" [routerLink]="[ '/', 'matches', item?.matchID ]">
我想它会解决问题
<ion-list *ngIf="matches.length > 0">
<ion-item button *ngFor="let item of matches" [routerLink]="[ '/', 'matches', item.matchID ]">