我正在使用访存进行API调用。成功进行API调用后,我得到了一个良好的状态代码,但是当尝试评估状态代码时,即使我的状态代码为“ 200”,我的“ if”语句也总是变为“ else”。请查看以下代码段:
const fetch = require("node-fetch");
var accessToken = "Bearer <ACCESS TOKEN>";
var url = '<URL>';
var headers3 = {
'Authorization': accessToken
};
const delAPI = async url => {
try {
const response = await fetch(url, {method: "DELETE", headers: headers3});
const res = await response.status;
console.log(response.status);
return res.status;
} catch (error) {
console.log(error);
}
};
delAPI(url).then(
status => {
const res = status;
if (res === 200) {
console.log("Integration has been deleted");
} else {
console.log("Integration is either disabled or not installed");
}
});
我认为问题出在条件“(res === 200)”之内,并且它可能未将“ res”评估为来自异步函数delAPI的实际response.status?我尝试了无数次迭代来解决此问题,但到目前为止还没有运气。感谢您的协助,以使我朝正确的方向前进。...
答案 0 :(得分:0)
const res = await response.status;
console.log(response.status);
return res.status;
您的问题是变量res已经在其中包含了response.status http代码,但是您再次返回res.status,该变量应该是未定义的。
您只想返回res或response.status。顺便说一句,我认为您不需要等待response.status。这不是一个承诺。
另一方面,我们可以在此处进行一些重构:
status => {
if (status === 200) {
console.log("Integration has been deleted");
} else {
console.log("Integration is either disabled or not installed");
}
});