0

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

1 Answer 1

1

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".

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.