when accessing an object field using get method,value always undefined


when accessing an object field using get method,value always undefined



i wrote a TypeScript model and i wrote a get method :


get comments(): [any] {
return this.commentsTest;
}
set comments(comments: [any]) {
this.commentsTest = comments;
}



the field it self is defined as private :


private commentsTest: [any];



in the constructor i initialized the object as follow :


this.comments = commentsTest;



when i am trying to use that field using the get method it is always undefined,
but if i am accessing it directly(while getting access warning)
i do get the values i am looking for :


openExtendedDetailsModal(comments) {
console.log("***" + this.investorObject.comments); // undefined
console.log(this.investorObject.commentsTest); // defined
//this.commentsdDetails.altOpen(comments);
}



UPDATE
here is the code i am using :


investorObject: Investor;
prametersSubscription: Subscription;
id: string;

constructor(private investorService: InvestorsService, private route: ActivatedRoute) {}

ngOnInit() {
this.prametersSubscription = this.route.params.subscribe(
(params: Params) => {
this.id = params.id;
this.getInvestorData();
}
);
}

ngOnDestroy() {
this.prametersSubscription.unsubscribe();
}

getInvestorData() {
this.investorService.getSingleInvestorById(this.id).subscribe(
data => {
this.investorObject = data.data;
console.log(this.investorObject.commentsTest);
});
}

openExtendedDetailsModal(comments) {
console.log("***" + this.investorObject.lastName); // defined
console.log("***" + this.investorObject.comments); // undefined
console.log(this.investorObject.commentsTest); // defined
//this.commentsdDetails.altOpen(comments);
}



here is part of the Investor class :
enter image description here
what am i missing here ?



Thanks





How is this.investorObject declared?
– tymeJV
Jul 1 at 13:08


this.investorObject





Where is this.commentsTest initialized to a value?
– Pointy
Jul 1 at 13:09


this.commentsTest





yes.all other fields are populated and the data is available. for example i am also using : this.investorObject.lastName which is also has set/get and i do no have that problem. it is getting the data from http call the entire object is populated except this field @Pointy
– Uriel Parienti
Jul 1 at 13:11






Well because you have not posted that code, it's very difficult to help.
– Pointy
Jul 1 at 13:12





added the code. the comments i am sending to "openExtendedDetailsModal" is just something i tried ti test,it should not get value and instead just using "this.investorObject.comments"
– Uriel Parienti
Jul 1 at 13:17




1 Answer
1



i manged to solve this issue,



i have changed the model as followed :



1)changed the constructor


constructor() {}



2)changed the name and the type of the commentsTest field:


private commentsTest: [Object];



3)Initiated as followed :


this.investorObject = new Investor();
Object.assign(this.investorObject, data.data as Investor);



so now ,i am getting the comments as i wanted to,also the type of the object is Investor.



enter image description here



Thanks all






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Rothschild family

Cinema of Italy