0

My Angular application received response from server in JSON format. I convert the response into a model like follows

let questionListSubscription = this.questionManagementService.questionsArray$.subscribe((result: Result) => {
        console.log("received result from question array observable", result);
        if (result.result === "success") {
            //received response from server
            let questionList = JSON.parse(result.additionalInfo) as PracticeQuestionsListAPI;
            console.log("got list of questions value ", questionList);
            //...
            this.questionsFilter["pagination-info"] = questionList["pagination-info"];
            //...
        }
    }
);

The above randomly crashed with error ERROR TypeError: Cannot set property 'pagination-info' of undefined pointing to line this.questionsFilter['pagination-info'] = questionList['pagination-info'];

When I ran the code again, it worked.

Is the above assignment wrong and could potentially lead to memory leak?

Evaldas Buinauskas
  • 13,739
  • 11
  • 55
  • 107
Manu Chadha
  • 15,555
  • 19
  • 91
  • 184
  • 2
    When is the field/property `questionsFilter` set? Is it also done in a call back that executes concurrently with the call back to `questionsArray`? If so you have a race condition and you must chain your observables so that the callbacks execute in a specific order. Also this has nothing to do with a memory leak, that is a completely unrelated topic (google it to see what that entails). – Igor Aug 28 '19 at 15:31

1 Answers1

0

I think the problem is you are not defined the this.questionsFilter correctly. It should be defined as:

questionsFilter = {}
Abel Valdez
  • 2,368
  • 1
  • 16
  • 33
  • In `Angular6`, I have defined it as `export class PraticeQuestionListComponent implements OnInit {questionsFilter:GetQuestionsfilter ; }`. Now I have also initialised it as `this.questionsFilter = new GetQuestionsfilter("",new AdditionalPagingInfo("",new PartitionInfo(0,0)));` in constructor. I am wondering is just assigning it to the incoming response is incorrect in some way – Manu Chadha Aug 28 '19 at 17:04
  • Then check if the questionList['pagination-info']; is getting the value from API – Abel Valdez Aug 28 '19 at 19:36