I am facing one issue that is I have created a functional component for API calling where I am getting params and API name as function parameter. Inside this method I am calling API with fetch & I am getting response too but when I am returning the response then it showing me undefined. What is the issue in it?
let formData = new FormData();
formData.append("apikey", Constant.API_KEY);
formData.append("email", email);
ApiCall.ApiCalling(formData, Constant.forget_password)
.then((data) => {
console.log('Response -> ', data); // Here getting undefined
})
// New component ApiCall.js
export default {
async ApiCalling(params, apiName) {
await NetInfo.fetch().then(async state => {
if (state.isConnected == true) {
console.log(Constant.BASE_URL + apiName);
console.log(params);
await fetch(Constant.BASE_URL + apiName, {
method: 'POST',
body: params
})
.then(results => results.json())
.then((response) => {
console.log("Response - ", response); // Here getting
proper response
return response;
})
.catch(function (error) {
console.log('There has been a problem with your fetch
operation: ' + error.message);
throw error;
});
} else {
alert("Please connect with internet")
}
});
}
}
// New component ApiCall.js
export default {
async ApiCalling(params, apiName) {
var result; // Create one variable
await NetInfo.fetch().then(async state => {
if (state.isConnected == true) {
console.log(Constant.BASE_URL + apiName);
console.log(params);
await fetch(Constant.BASE_URL + apiName, {
method: 'POST',
body: params
})
.then(results => results.json())
.then((response) => {
console.log("Response - ", response);
result = response; // Assign response to that variable
})
.catch(function (error) {
console.log('There has been a problem with your fetch
operation: ' + error.message);
throw error;
});
} else {
alert("Please connect with internet")
}
});
return result; // Return the response
}