I am doing a post, I get the information. But after I do a check to know if is there any error in the fetch it crashes because it receives undefined.
Error:
TypeError: undefined is not an object (near '...}).then(function (resp...')
what it returns the fetch
Object {"accessToken": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1c2VyIjoiQWFhYWFzcyIsImlhdCI6MTYxMjM1OTQ0M30.g18clMLcREy5cT6FwConZBpV_NJ69mwP56ddzQAhl50","id": "601aa71368c81c003324ff37"}
fetch function
export function registerNewUser(data) {
const url = "http://localhost:8080/user/register";
return axios.post(url, data)
.then((res) => {
console.log("fetch in")
console.log(res.data)
return res.data
}).catch(() => {
console.log("error")
return {accessToken: null, id: -1}
})
}
Button to sign up
<TouchableOpacity
style={styles.loginScreenButton}
underlayColor='#fff'
onPress={async () => {
console.log("Pseudo: " + pseudo + "\nPassword: " + password);
setPseudoError(checkPseudo(pseudo));
setPasswordError(checkPassword(password));
if (checkPseudo(pseudo) === "" && checkPassword(password) === "") {
Promise.then(await registerNewUser({
username: pseudo,
password: password,
enrollment: 1
})).then(response => {
console.log("response: " + response)
if (response.id === -1) {
setPseudoError('Invalid pseudo or password')
setPasswordError('Invalid pseudo or password')
} else
navigation.navigate('Home', response)
})
}
}
}
>