0

I am working on an angular application. I have a method in which I am trying to assign values to the variables from the data received from API call. code

this.adminService.getConfigurations(input).subscribe(
    data => {
        this.data_Configuration = data[0];
        this.data_Configuration_e = data[1]     
        this.data_Configuration.forEach(itm => {
            itm.ApplicableDetails = []
            
            this.data_Configuration_e
                .filter(ent => ent.Id == itm.Id)
                .forEach(e => {itm.ApplicableDetails.push(e)});
                        
            itm.ApplicableLevel.FirstFl = itm.ApplicableDetails.some(e => e.Nm == 'First') ? 'Y' : 'N'             
            itm.ApplicableLevel.SecondFl = itm.ApplicableDetails.some(e => e.Nm == 'Second') ? 'Y' : 'N'
        });

My data_Configuration variable is defined from a interface

public data_Configuration : IConfiguraion[] = []

export interface IConfiguration {
  Id: string;
  Nm: string;
  ApplicableLevel?: IApplicableLevels;
  ApplicableDetails?: IDetails [];
}

export interface IApplicableLevels {
    FirstFl?: string;
    SecondFl?: string;
}

When I try to assign values to FirstFl using itm.ApplicableLevel.FirstFl = itm.ApplicableDetails.some(e => e.Nm == 'First') ? 'Y' : 'N' line I get an error cannot property FirstFl of undefined. How or where should I initialize the value of FirstFl to avoid this error?

7
  • The error doesn't mean that FirstFl is undefined, it means that itm.ApplicableLevel is undefined. You'll probably want to insert itm.ApplicableLevel = {}; before assigning to it. Commented Oct 17, 2020 at 8:46
  • The interface is called IPrgmConfiguration but the type definition is IConfiguraion. Which I assume is a typo while posting the question? Commented Oct 17, 2020 at 8:48
  • @MichaelD Yes that's a typo. edited Commented Oct 17, 2020 at 8:50
  • 1
    {} is an object with named properties, [] is an array with index numbered elements. Commented Oct 17, 2020 at 8:52
  • 1
    use itm.ApplicableLevel=itm.ApplicableLevel || {} as IApplicableLevels before assign FirstFl. When we use interface, some like {} as IConfiguration, the variable has all the properties of the interface but they are undefined Commented Oct 17, 2020 at 9:00

1 Answer 1

1

Most probably it's a TS Lint error because the itm.ApplicableLevel isn't initialized yet. You could instead try to initialize the ApplicableLevel property with both it's sub-properties FirstFl and SecondFl as an object instead.

Also you could skip an additional level of iterations of the data_Configuration_e array by replacing the unnecessary filter with an if in the forEach loop.

Try the following

this.data_Configuration.forEach(itm => {
  itm.ApplicableDetails = [];
  
  this.data_Configuration_e.forEach(e => {
    if (ent.Id == itm.Id) {
      itm.ApplicableDetails.push(e);
    }
  });

  itm.ApplicableLevel = <IApplicableLevels>{
    FirstFl: (itm.ApplicableDetails.some(e => e.Nm == 'First')) ? 'Y' : 'N',
    SecondFl: (itm.ApplicableDetails.some(e => e.Nm == 'Second')) ? 'Y' : 'N'
  };
});
Sign up to request clarification or add additional context in comments.

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.