Reputation: 79
I have function that fetches data from api. I need to return the data to a variable declared inside the in class. Below is the the function
getColumns(){
let auth_token='xkjhkdfdfdf';
let url=this.api.apiList.contact_list;
var columns={};
this.common.getRequestTable(url,auth_token).subscribe(
res => {
console.log(res);
columns=res.column_heading;
return columns;
})
}
i have to pass the value to a variable declared in the class
columnList = {}
But when is try like
columnList=this.getColumns();
columnList is undefined
Upvotes: 0
Views: 60
Reputation: 1112
Define your columns variable at class level :
let auth_token='xkjhkdfdfdf';
let url=this.api.apiList.contact_list;
var columns={};
// If your common is service where you are hitting your backend URL then try
getColumns() : Subscription {
return this.common.getRequestTable(url,auth_token).subscribe(
res => {
console.log(res);
columns=res.column_heading;
return columns;
})
}
Although this is not a right way to pass auth token like you are providing, and also a suggestion Please keep you variables name proper so that they themselves convey their use. If "common" is a service then please use "commonService".
Upvotes: 1