angular 9 ERROR TypeError: невозможно прочитать свойство 'title' неопределенной одной и той же ошибки несколько раз, но данные все равно отображаются - PullRequest
0 голосов
/ 27 мая 2020

У меня есть вызов веб-службы для вложенного объекта json

Survey[ 
   id, 
   title, 
   Sections:[
           id, 
           survey_id, 
           Questions:[ id, type, title, options[...]] 
           ] 
]

вызов службы

getSurvey(id:number):Observable<iSurvey>{
    var url = environment.apiUrl+'surveys/view/'+id;
    return this.http.get<iSurvey>(url);
}

compoent.ts

public survey = null; //survey variable
ngOnInit(): void {
    this.serveyServ.getSurvey(1).subscribe(res=>this.survey=res);
}

компонент. html

<div class="container">
    <h1 class="text-center survey-title">{{survey.Survey.title}}</h1>
    <p class="text-center survey-welcome">{{survey.Survey.welcome}}</p>

    <div *ngFor="let section of survey.Section; let is = index" class="section">
        <h2 class="text-center">{{section.Section.title}}</h2>
        <p class="text-center">{{section.Section.welcome}}</p>
        <div class="questions">
            <div *ngFor="let question of section.Section.Question; let iq = index" class="question">
                <div class="qtitle bold">
                    <span class="qNo">{{(is+1)+'.'+(iq+1)}}</span>
                    {{question.Question.title}}
                </div>               
            </div>
        </div>
    </div>
</div>

есть 2 проблемы
1 - отображаются только данные верхнего уровня (опрос), а не разделы и вопросы и любые другие вложенные данные
2 - даже отображается только опрос Данные уровня, но все еще в консоли, они отображаются Cannot read property 'title' of undefined 20 раз, иногда 24 раза меняется при каждом обновлении sh, но в браузере они отображаются.

ERROR TypeError: Cannot read property 'title' of undefined
    at SurveyComponent_Template (survey.component.html:2)
    at executeTemplate (core.js:12129)
    at refreshView (core.js:11968)
    at refreshComponent (core.js:13449)
    at refreshChildComponents (core.js:11689)
    at refreshView (core.js:12024)
    at refreshDynamicEmbeddedViews (core.js:13360)
    at refreshView (core.js:11995)
    at refreshComponent (core.js:13449)
    at refreshChildComponents (core.js:11689)
...