Hello, I'm currently diving into Angular and have encountered an issue with a class level variable called moratoriumID in my component. I have a method that makes a POST request and assigns the returned number to moratoriumID. Everything seems to work fine as I can see the number in the chrome debugger console. However, when I try to access the variable later in the code, it shows up as undefined. What am I missing here? How can I properly utilize the moratoriumID later on? Any insights or explanations would be greatly appreciated. --Jason
export class AddMoratoriumsComponent implements OnInit {
moratoriumID: number;
//The PostMoratorium method is supposed to assign the moratoriumID
PostMoratorium(moratoriumtopost: Moratorium):void {
this.moratoriumService.PostMoratorium(moratoriumtopost)
.pipe(takeUntil(this.ngUnsubscribe))
.subscribe((data) => (this.moratoriumID) = (data),
(error) => (console.log(error)),
() => console.log('Post moratorium is complete', this.moratoriumID),
);}
//...The PostMoratorium method is being invoked here
this.PostMoratorium(moratoriumtopost);
//Later on, trying to use moratoriumID but receiving undefined error - why?
if (this.selectzipvalues.length>0){
const res = this.isoLocs.filter(i => this.selectzipvalues.includes(i.zipCode));
res.forEach(myFunction);
function myFunction(item) {
const moratoriumlocationstopost = {
county:item.county,
city:item.city,
zip:item.zipCode,
moratoriumId:this.moratoriumID, //IT SAYS ITS UNDEFINED ..WHY ?
} as MoratoriumLocation;
}}