After encountering the issue of
core.umd.js:3523 ORIGINAL EXCEPTION: Cannot read property 'fullName' of undefined
I realized that the Exception stemmed from a Template trying to access a specific property:
{{project.collaborators["0"]["fullName"]}}
While searching for solutions, I came across a helpful answer.
However, I am interested in creating a global service that can automatically replace missing or empty properties with a default value, such as -
This would streamline the code and reduce the chances of bugs.
// undefined-obj.service.ts
import { Injectable } from '@angular/core';
@Injectable()
export class UndefinedObjectsGlobalService {
private charecterToReplace: string = '-'; // set default value
replaceDefaultCharacter(object: any, characterToReplace: string): any {
this.characterToReplace = characterToReplace;
// create instance vars to store keys and final output
let keyArr: any[] = Object.keys(object),
var dataArr: any[];
// loop through the object,
// pushing values to the return array
keyArr.forEach((key: any) => {
// if key is null at any iteration then replace it with the given character
if (key == null){
dataArr.push(object[key] = this.characterToReplace);
// else push
} else{
dataArr.push(object[key]);
}
});
// return the resulting array
// need to convert it back to object any idea ?
return dataArr;
}
}
Since I am new to Angular, the class UndefinedObjectsGlobalService
may contain bugs. I would appreciate any help.