I want to return an object
after a successful API call from another file.But everytime it call sucessfully,but the object that return is Undefined
. Let me explain the situation clearly.
App.js
From App.js
I calling Auth.session()
which is will suppose to return a Session()
object.
try{
Auth.session(); #data123
console.log("data 123",Auth.session());
}catch (e) {
console.log(e);
}
Auth.js
export class Auth {
static session() {
if(token !=null){
return Session.getSession(); //DATA 456
//Here when return to App.js,is the outcome I want
}
if(refreshToken != null){
try{
await axiosAccessClient.post(url, body)
.then(function (response) {
//... do something else with the response.data
console.log(Session.getSession()); //here have the outcome I want
return Session.getSession(); // **DATA 678**
//but here when return to App.js,it become undefined
}).catch(function (error) {
throw error;
})
} catch (e) {
throw e
}
}else{
thorw "No session"
}
}
}
Here is my Session
class look like
export class Session {
constructor(access_token){
this.token= access_token;
}
static getSession(){
return new Session(
localStorage.getItem("access_token"),
)
}
}
So I which in App.js
,I can get the console.log
like this :
Session{access_token: .......} //Should return this in App.js
I can get the above result from if(token!= null)
(DATA 456) part,but in the try
block for the API call (DATA 678 PART) it return UNDEFINED
in App.js
.
So my question is ,how to return an object after successfully API call? I trying my best to address the problem,any help will be appreciated.