In my app, the login function sends the phone number inserted by the user, and if the server returns "success" it is supposed to navigate to another screen. Unfortunately the app returns "Undefined is not an object (evaluating 'navigation.navigate')".
The code is the following:
const login = (mobileNumber, navigation) => {
Object.keys(mobileNumber).forEach((key) => {
console.log(mobileNumber[key]);
});
if (mobileNumber.mobileNumber == null) {
alert('Please fill Mobile number');
return;
}
let dataToSend = {mobileNumber: mobileNumber.mobileNumber};
let formBody = [];
for (let key in dataToSend) {
let encodedKey = encodeURIComponent(key);
let encodedValue = encodeURIComponent(dataToSend[key]);
formBody.push(encodedKey + '=' + encodedValue);
}
formBody = formBody.join('&');
fetch('https://mywebsite.com/api/v1/login.php', {
method: 'POST',
body: formBody,
headers: {
//Header Defination
'Content-Type':
'application/x-www-form-urlencoded;charset=UTF-8',
},
})
.then((response) => response.json())
.then((responseJson) => {
console.log(responseJson);
// If server response message same as Data Matched
if (responseJson.status === 'success') {
alert("Login OK!");
navigation.navigate('FeedScreen');
} else {
console.log('Please check mobile number');
}
})
.catch((error) => {
//Hide Loader
console.error(error);
});
}