I'm new to angular and I've been trying to use http.get to get data from an API and assign it to a value in a component
This is the method which is used to get data
  public getByUserName(username:String):User{
     let res:CommonResponse;
     this.http.get<CommonResponse>(BASE_URL+'/'+username)
    .subscribe(  (response) => { res = response },
      error => this.errorHandler.handleError(error)
    )
    console.log("Response "+ res)
    return res.responseObj;
  }
When I print the result inside the subscribe function I get the result
But when I've tried to assign it to a local variable and return responseObj from res.
but the res object is undefined. Is there any other way to archive this functionality
 
     
     
    