提问人:Deveshi Singh 提问时间:11/2/2023 更新时间:11/2/2023 访问量:18
我的错误消息未返回,仅显示默认错误消息
My error message is not being returned, the default error message is only being displayed
问:
在下面的代码中,当我捕获错误时,不会返回 getJSON 中定义的错误消息,仅返回默认值。我该如何解决这个问题,这是输出
const getJSON=function(url,errMsg='something went wrong'){
return fetch(url)
.then((res)=>{
if(!res.ok){
throw new Error(`${errMsg} ${res.status}`);
}
return res.json();
})
}
const getCountryData=function(country){
getJSON(`https://restcountries.com/v3.1/name/${country}`,'country not found')
.then((data)=>{
renderCountry(data[0]);
const neighbor=data[0].borders[0];
if(!neighbor)throw new Error('no neighbor found');
return getJSON(`https://restcountries.com/v3.1/alpha/${neighbor}`,'country not found')
})
.then((data)=>{
renderCountry(data[0],'neighbour');
})
.catch(err=>{
console.log(err);
renderError(`oops ${err} Try again`);
})
.finally(()=>{
countriesContainer.style.opacity = 1;
})
}
btn.addEventListener('click',function(){
getCountryData('australia');
});
我遵循了通常的语法,预期的输出应该是“哎呀,找不到邻居,再试一次”
答: 暂无答案
评论
0
undefined
data
data[0].borders
undefined
country
data[0].borders