如何return响应另一个功能组件
How to return response to another functional component
我面临一个问题,即我已经为 api 调用创建了一个功能组件,我在其中获取参数和 api 名称作为函数参数。在这个方法中,我用 fetch 调用 api 并且我也收到了响应,但是当我返回响应时,它显示我未定义。它有什么问题?
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
}
我面临一个问题,即我已经为 api 调用创建了一个功能组件,我在其中获取参数和 api 名称作为函数参数。在这个方法中,我用 fetch 调用 api 并且我也收到了响应,但是当我返回响应时,它显示我未定义。它有什么问题?
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
}