gpt4 book ai didi

javascript - 错误类型错误 : Cannot read property 'length' of undefined at Object. updateDirectives

转载 作者:行者123 更新时间:2023-12-02 23:15:25 25 4
gpt4 key购买 nike

我有一个 Angular 8 应用程序。如果加载某个组件,我会收到错误

无法读取未定义的属性“长度”

我用谷歌搜索了很多并做了一些教程。但我不知道如何处理这个错误。

所以如果我加载这个组件:

import { Component, OnInit } from '@angular/core';
import { HealthAPIService } from '../../shared/health-api/health-api.service';

import { DossierEntry } from '../../interfaces/dossier/dossier-entry.interface';

@Component({
selector: 'app-dossier-correspondence',
templateUrl: './dossier-correspondence.component.html',
})

export class DossierCorrespondenceComponent implements OnInit {
allCorrespondence: Array<DossierEntry>;
correspondenceEntries: Array<DossierEntry>;
attachmentEntries: Array<DossierEntry>;
message = '';
emptyMessage = 'Geen correspondentie.';
errorMessage = 'Er ging iets mis met de connectie. Probeer over enkele minuten nogmaals.';

correspondenceLoaded = false;
showingSingle = false;

single: DossierEntry;

constructor(private healthAPIService: HealthAPIService) {}

handleCorrespondenceLoad(result) {
if (result && result.length === 0) {
this.message = this.emptyMessage;
return;
}
this.allCorrespondence = result;
this.attachmentEntries = [];
this.correspondenceEntries = [];
for (let entry of result) {
switch (entry.type) {
case 'correspondence': {
this.correspondenceEntries.push(entry);
break;
}
case 'attachments': {
this.attachmentEntries.push(entry);
break;
}
default: {
console.log('Dossier correspondence heeft een invalide entry soort teruggegeven');
break;
}
}
}
}

gotoItem(index, type: string) {
this.showingSingle = true;
// this.single = this.allCorrespondence[index];
switch (type) {
case 'correspondence': {
this.single = this.correspondenceEntries[index];
break;
}
case 'attachments': {
this.single = this.attachmentEntries[index];
break;
}
default: {
console.log('Er is op een ongeldige soort dossier entry geklikt');
break;
}
}
}

goBack(event) {
this.showingSingle = false;
}

ngOnInit() {
this.healthAPIService.getDossierEntry('correspondence').subscribe(result => {
console.log(result);
this.handleCorrespondenceLoad(result), (this.correspondenceLoaded = true);
}, msg => (this.message = this.errorMessage));
}
}


然后我会得到这个错误:

dossier-correspondence.component.html:6 ERROR TypeError: Cannot read property 'length' of undefined
at Object.updateDirectives (dossier-correspondence.component.html:8)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:30537)
at checkAndUpdateView (core.js:29933)
at callViewAction (core.js:30174)
at execComponentViewsAction (core.js:30116)
at checkAndUpdateView (core.js:29939)
at callViewAction (core.js:30174)
at execEmbeddedViewsAction (core.js:30137)
at checkAndUpdateView (core.js:29934)
at callViewAction (core.js:30174)

在这一行:

<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>

这是应用程序正在加载组件的代码:

export class IsLoadingComponent implements OnInit {
@Input() message: string;

public text: string;

constructor() {}

ngOnInit() {
this.text = this.message + '...';
}
}

谢谢

现在还有 html 文件:

<app-vital10-page [noTopBar]="true">
<h2 class="section-header">Correspondentie</h2>

<p class="data-entry" *ngIf="!allCorrespondence">{{ message }}</p>

<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>

<div *ngIf="!showingSingle && correspondenceEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Algemeen</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>

<div *ngIf="!showingSingle && attachmentEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Bijlage</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of attachmentEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>

<app-dossier-correspondence-item
[item]="single"
(goBack)="goBack($event)"
*ngIf="showingSingle">
</app-dossier-correspondence-item>
</app-vital10-page>

但是我会在这一行收到错误:

<div *ngIf="!showingSingle && correspondenceEntries && correspondenceEntries.length > 0">

html 文件现在看起来像这样:

<app-vital10-page [noTopBar]="true">
<h2 class="section-header">Correspondentie</h2>

<p class="data-entry" *ngIf="!allCorrespondence">{{ message }}</p>

<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>

<div *ngIf="!showingSingle && correspondenceEntries && correspondenceEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Algemeen</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of correspondenceEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>

<div *ngIf="!showingSingle && attachmentEntries.length > 0">
<div class="main-row main-row-dossier">
<section class="data-entry">
<h3 class="dossier-header">Bijlage</h3>
<table class="dossier-table">
<thead class="dossier-tableheader">
<tr>
<th class="dossier-tablehead fixed-one-fifth">Datum</th>
<th class="dossier-tablehead fixed-four-fifths">Onderwerp</th>
</tr>
</thead>
<tbody class="dossier-tablebody">
<tr class="dossier-correspondencerow" *ngFor="let entry of attachmentEntries; let i = index" (click)="gotoItem(i, entry.type)">
<td>{{ entry.date | date:"dd-MM-y" }}</td>
<td>{{ entry.name }}</td>
</tr>
</tbody>
</table>
</section>
</div>
</div>

<app-dossier-correspondence-item
[item]="single"
(goBack)="goBack($event)"
*ngIf="showingSingle">
</app-dossier-correspondence-item>
</app-vital10-page>

出现错误:

dossier-correspondence.component.html:8 ERROR TypeError: Cannot read property 'length' of undefined
at Object.updateDirectives (dossier-correspondence.component.html:30)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:30537)
at checkAndUpdateView (core.js:29933)
at callViewAction (core.js:30174)
at execComponentViewsAction (core.js:30116)
at checkAndUpdateView (core.js:29939)
at callViewAction (core.js:30174)
at execEmbeddedViewsAction (core.js:30137)
at checkAndUpdateView (core.js:29934)
at callViewAction (core.js:30174)

最佳答案

尝试更改模板片段

<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>

<div *ngIf="!showingSingle && correspondenceEntries.length > 0">

<app-is-loading *ngIf="!correspondenceLoaded" message="Correspondentie wordt geladen"></app-is-loading>

<div *ngIf="!showingSingle && correspondenceEntries && correspondenceEntries.length > 0">

您似乎正在尝试访问 correspondenceEntries.length,但它尚未定义

关于javascript - 错误类型错误 : Cannot read property 'length' of undefined at Object. updateDirectives,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57184666/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com