Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

720
Views
Uncaught (in promise) TypeError: Cannot convert undefined or null to object - angular 9

I have a service call that makes a call to an API to return JSON. I then have a function with a subscribe that calls the service call. The JSOON is returned fine and the service call and subscribe all work. However, upon building my application in its build pipeline, I get the following error "Uncaught (in promise) TypeError: Cannot convert undefined or null to object." and my build fails (even though it works fine locally and passes all tests). I am using angular 9 and typescript.

My subscribe and the function that it's in look as such:


houseData: HouseValueObject;
houseNumber = '12345';

callHouseService() {
   this.streetHouseService.getHouseData(this.houseNumber).subscribe(
     async (data) => {
       this.houseData = await data;
       const keys = Object.keys(this.houseData.extensiveHouseData);
       this.extensiveHouseDataList = keys.map(k => this.houseData.extensiveHouseData[k]);
}
)
}

The getHouseData method looks as such:

export class HouseService {
  private readonly relativeURL = 'services/api/houseinfo/'
  
  constructor(private http: HttpClient) {
  
  getHouseData(houseNumber: string): Observable<HouseValueObject> {
   return.this.http.get<HouseValueObject>(this.relativeURL + houseNumber + '/');
}
}
}

The value object for HouseValueObject looks as such:

export class HouseValueObject {
   numberBathrooms: number;
   extensiveHouseData: HouseValue[];
}

I thought adding the async and await would suspend execution until the returned promise is fulfilled so that the JSON data would be delivered to const keys = Object.keys... line of code would work, but that does not appear to be the case. My question is, how can I correctly go about suspending execution until the returned promise is fulfilled to ensure the JSON data exists and the const keys = Object.keys... line of code works as it does locally?

over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I personaly would rewrite the service to return a promise, should be easier to read.

HouseService:

getHouseData(houseNumber: string): Promise<HouseValueObject> {
   return this.http
      .get<HouseValueObject>(`${this.relativeURL}${houseNumber}/`)
      .toPromise<HouseValueObject>();
}

Component:

async callHouseService(): Promise<void> {
  this.houseData = await his.streetHouseService.getHouseData(this.houseNumber);
  this.extensiveHouseDataList = Object
    .keys(this.houseData.extensiveHouseData)
    .map(k => this.houseData.extensiveHouseData[k]);
}
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!