I have a API class that gets data from an API and needs to return two pieces of information. I used the following function to get the data, which gets the Json and puts the data into a helper class and returns that class:
async register(name: string, username: string, email: string, password: string) : Promise<AuthResponse> {
const authResponse = new AuthResponse();
const body = {
'name': name,
'username': username,
'email': email,
'password': password
};
this.log.info('calling REST API at:' + this.API_URL);
this.log.info('with a requestBody:' + body);
axios.post(this.API_URL + '/', body)
.then(response => {
this.log.info(response.data);
this.log.info(response.status);
this.log.info(response.headers);
authResponse.tokin = response.data.token;
authResponse.role = response.data.role;
})
.catch(error => {
this.log.error("Error sending data: ", error);
});
return authResponse;
}
I call that function here:
authService.login(email, password )
.then((result : AuthResponse) => {
// Set our context state
setAuthState({
token: result.token,
authenticated: true,
});
//set role in state
setRole(result.role);
// Set our HTTP Headers
axios.defaults.headers.common['Authorization'] = `Bearer ${result.token}`;
// Write the JWT to our secure storage
await SecureStore.setItemAsync(TOKEN_KEY, result.token);
return result;
}); //then
I get the following error:
Argument of type '(result: AuthResponse) => AuthResponse' is not assignable to parameter of type '(value: void) => AuthResponse | PromiseLike<AuthResponse>'.
Types of parameters 'result' and 'value' are incompatible.
Type 'void' is not assignable to type 'AuthResponse'.ts(2345)
a working program.
3