I am facing a challenge in my Angular project where I need a property within a class to return specific fields in an object. Although I have implemented this successfully in .Net before, I am encountering an issue with getting an "Undefined" value returned.
Even though I can confirm that the properties (transLanguageId, transLangDesc, translation) are populated on the IBatchFile, they do not seem to be coming back in the GET request. Additionally, not even a console.log statement appears in the GETTER. It seems like the GETTER code is not being accessed.
What could be causing this problem?
Any help or guidance on this matter would be greatly appreciated.
model.ts
export class TranslationItem {
id: number;
language: string;
translation: string;
}
export class IBatchFile {
constructor(_transData: TranslationItem) {
this._transData = new TranslationItem();
}
private _transData: TranslationItem;
get transData(): TranslationItem {
this._transData.id = this.transLanguageId;
this._transData.language = this.transLangDesc;
this._transData.translation = this.translation;
return this._transData;
};
set transData(value: TranslationItem) {
this._transData.id = value.id;
this._transData.language = value.language;
this._transData.translation = value.translation;
};
transLanguageId: number;
transLangDesc: string;
translation: string;
}
batchdocList.ts
private _batchFileListResults$ = new BehaviorSubject<IBatchFile[]>([]);
public loadDocList(batchid) {
this.urlservice.getBatchFiles(batchid)
.subscribe(result => {
this._batchFileListResults$.next(result); //**result is of class IBatchFile**
this._batchFileListResults$.value.forEach(item => {
console.log(item.transData); //**returns Undefined**
});
}
url.service.ts
getBatchFiles(batchId: number) {
return this.dataservice.getBatchFiles(config.resources.Api.gatewayUri + config.resources.Api.getBatchFiles+"/"+batchId);
}
data.service.ts
getBatchFiles(url:string): Observable<IBatchFile[]> {
return this.http.get<IBatchFile[]>(url)
.pipe(map(response => response))
.pipe(catchError(this.handleError));
}