Я работаю над приложением Angular 2. У меня есть объект с вложенным объектом «subQuestions». Мне нужно присвоить ему значения, но я получаю ошибку, так как subQuestionId не распознается. subQuestionId является свойством subQuestions
ошибка
TypeError: Cannot set property 'subQuestionId' of undefined
класс данных
export class AnswerEntryDataModel{
consultationId:string;
responseId:string;
questionId:string;
answers:string[];
isPreDefineAnswer:boolean;
subQuestions:{
subQuestionId:string;
isPreDefineSubAnswer:boolean;
subQuestionAnswers:string[];
}
}
Компонент
export class myComponent implements OnInit {
private answerData = new AnswerEntryDataModel();
private answersList:string [] = [];
private subQuestionAnswersList:string [] = [];
private assembleAnswer( responseId:string, questionId:string, subQuestionId:string, subQuestionInputType:string, subQuestionAnswersList:string[] ):AnswerEntryDataModel
{
this.answerData.answers = [];
this.answerData.consultationId = this.session.getItem('consultationId');
this.answerData.responseId = responseId;
this.answerData.questionId = questionId;
if(subQuestionId!=null || subQuestionId!="undefined")
{
answerData.subQuestions.subQuestionId = subQuestionId; //throw error from here
if(subQuestionInputType =="textbox"){
this.answerData.subQuestions.isPreDefineSubAnswer = false;
}
else {
this.answerData.subQuestions.isPreDefineSubAnswer = true;
}
if(subQuestionAnswersList.length>0)
{
for(var itemIndex in subQuestionAnswersList)
{
this.answerData.subQuestions.subQuestionAnswers.push(subQuestionAnswersList[itemIndex]);
}
}
}
console.log("***** complete answer data ", this.answerData);
return this.answerData;
}