我正在使用axios来获取API,并且需要根据返回的数据包括一个条件:
axios.get('https://api.url.endpoint).then(response => ())
我将如何根据响应做出条件语句(特别是检查响应的属性是否返回null)?
感谢您的帮助。
在以下响应中使用if..else语句不起作用。
axios.get('https://api.url.endpoint).then(response => (
if (response.data != null) { }
))
答案 0 :(得分:2)
如果结果不是 null
,如果该概念仍然会引发错误,则
axios.get('...')
.then(res => {
if (!res.data) throw new Error('No data');
return res.data
});
尽管如此,我认为使用async
/ await
const res = await axios.get('...');
if (res.data) {
// happy day scenario
} else {
// bad day scenario
}