Mayroy
Mayroy

Reputation: 9

ERROR ReferenceError: countryCallingCode is not defined

I got error when try to set value for my property countryCallingCode which in first option not exist.

this.allData.customerFacingPhone.countryCallingCode = newItem.countryCallingCode

I got error:

ERROR ReferenceError: countryCallingCode is not defined

When try to console.log(allData);

I don't have customerFacingPhone ... I want to create property of allData customerFacingPhone and .countryCallingCode and set value...

Upvotes: 0

Views: 37

Answers (1)

Barremian
Barremian

Reputation: 31125

Most probably you haven't declared the type. Try to declare the type like below

export SomeComponent {
  allData: {
    customerFacingPhone: {
      countryCallingCode: any; // <-- elegant would be to define correct type here
    }
  } = Object.create(null);     // <-- initializing empty object

  // some example function
  someFunction() {
    this.allData.customerFacingPhone.countryCallingCode = newItem.countryCallingCode;
  }
}

More elegant way would be to use a TS Interface to define the type.

data.ts

export interface Data {
  customerFacingPhone: Phone;
  someOtherProperty: string;
}

export interface Phone {
  countryCallingCode: string;
  someOtherProperty: number;
}

And use it in the controller

import { Data, Phone } from './data';

export SomeComponent {
  allData: Data = Object.create(null);     // <-- initializing empty object

  // some example function
  someFunction() {
    this.allData.customerFacingPhone.countryCallingCode = newItem.countryCallingCode;
  }
}

Upvotes: 1

Related Questions