2

I looping through array of object and creating instance of specific class, followed by assigning values accordingly. Everything working fine, expect I need to check if value does exist then only assign.

In my scenario, I have list of questions which may or may not have answer so to check and assign value if not null, I doing following code but throwing error.

value: questionsList[key].answer.length>0? null : questionsList[key].answer[0].answerValue

complete code

if(questionElementType=="textbox")
 {       

  var isAnswerExist = questionsList[key].answer.length;
  if(isAnswerExist>0) // this one does work
  {
    var ans = questionsList[key].answer[0].answerValue;
    console.log("answer  ", ans);
  }


   let _textBox = new TextboxQuestion({
     questionId: questionsList[key].questionId,
     questionElementType: questionsList[key].questionElementType[0].title,        
     questionType: questionsList[key].questionType,
     title:questionsList[key].title,
     displayId: questionsList[key].displayId,
     key: questionsList[key].questionId, 
     label: questionsList[key].title,
     value: questionsList[key].answer.length>0? null : questionsList[key].answer[0].answerValue,  // need help here
     required: true,
     order: 5
    }); 

    this.mappedQuestions.push(_textBox);

 }

Error 1

formGroup expects a FormGroup instance. Please pass one in.

   Example:


<div [formGroup]="myGroup">
  <input formControlName="firstName">
</div>

In your class:

this.myGroup = new FormGroup({
   firstName: new FormControl()
});

Error 2

TypeError: Cannot read property 'answerValue' of undefined
4
  • what type of error? Commented Apr 10, 2018 at 9:19
  • I have update error at bottom my question Commented Apr 10, 2018 at 9:21
  • I think u shld reverse the condition, value: questionsList[key].answer.length>0? questionsList[key].answer[0].answerValue : null // need help here Commented Apr 10, 2018 at 9:24
  • correct, I just figure out, thanks Commented Apr 10, 2018 at 9:25

3 Answers 3

1

You should reverse your condition where you are getting the error,

You should add value only if array length > 0

value: questionsList[key].answer.length > 0 ?  questionsList[key].answer[0].answerValue : null,  // need help here

Full code

let _textBox = new TextboxQuestion({
     questionId: questionsList[key].questionId,
     questionElementType: questionsList[key].questionElementType[0].title,        
     questionType: questionsList[key].questionType,
     title:questionsList[key].title,
     displayId: questionsList[key].displayId,
     key: questionsList[key].questionId, 
     label: questionsList[key].title,
     value: questionsList[key].answer.length > 0 ?  questionsList[key].answer[0].answerValue : null,  // need help here
     required: true,
     order: 5
    }); 
Sign up to request clarification or add additional context in comments.

Comments

1

You just made a simple mistake there

value: questionsList[key].answer.length>0? null : questionsList[key].answer[0].answerValue,  // need help here

It Should be opposite

value: questionsList[key].answer.length>0?  questionsList[key].answer[0].answerValue : null ,

Comments

1

you need to do like this

value: (questionsList 
       &&  questionsList[key].answer  
       && questionsList[key].answer.length>0) ? questionsList[key].answer[0].answerValue : null 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.