Javascript 当用户输入无效的输入,并且由于该输入而无法从API获取时,如何向用户添加自定义错误

Javascript 当用户输入无效的输入,并且由于该输入而无法从API获取时,如何向用户添加自定义错误,javascript,error-handling,try-catch,try-catch-finally,Javascript,Error Handling,Try Catch,Try Catch Finally,我试图捕捉一个错误,这样如果有人输入了一个无效的位置,就会出现一条消息告诉用户该位置无效。有人能帮我吗?谢谢 const enquiryRef = document.querySelector('#enquiry'); const areaRef = document.querySelector('#area'); const dateRef = document.querySelector ('#day'); const weatherRef = document.querySelector

我试图捕捉一个错误,这样如果有人输入了一个无效的位置,就会出现一条消息告诉用户该位置无效。有人能帮我吗?谢谢

const enquiryRef = document.querySelector('#enquiry');
const areaRef = document.querySelector('#area');
const dateRef = document.querySelector ('#day');
const weatherRef = document.querySelector ('#conditions');
const tempRef = document.querySelector ('#temperature');
const tempDiffRef = document.querySelector ('#temperature-difference');
const searchRef = document.querySelector ('#searchBtn')
const api = {
  key: '03e844bff36a172614ecb788fd7c7fd8',
  base: 'https://api.openweathermap.org/data/2.5/',
};


enquiryRef.addEventListener('keypress', checkIfEnter);
searchRef.addEventListener('click', checkIfEnter);


function checkIfEnter(event){
  if(event.keyCode === 13 || event.type == 'click') {
    getResults(enquiryRef.value);
  }
}


function getResults(city){
  fetch(`${api.base}weather?q=${city}&units=metric&APPID=${api.key}`)
    .then((response) => response.json()) 
    .then((weatherData) => {
    displayResults(weatherData);
    });
}

function displayResults(weather) {
  areaRef.innerText = `${weather.name}, ${weather.sys.country}`;
  const dateToday = new Date();
  dateRef.innerText = dateToday.toDateString();
  tempRef.innerHTML = `${Math.round(weather.main.temp)}<span>°c</span>`;
  weatherRef.innerText = weather.weather[0].main;
  tempDiffRef.innerText = `${Math.round(weather.main.temp_min)}°c / ${Math.round(weather.main.temp_max)}°c`;
}

const inquiryref=document.querySelector(“#inquiry”);
const areaRef=document.querySelector(“#area”);
const dateRef=document.querySelector(“#day”);
const weatherRef=document.querySelector(“#条件”);
const tempRef=document.querySelector(“#temperature”);
const tempdref=document.querySelector(“#温差”);
const searchRef=document.querySelector(“#searchBtn”)
常量api={
键:“03e844bff36a172614ecb788fd7c7fd8”,
基数:'https://api.openweathermap.org/data/2.5/',
};
InquiryRef.addEventListener('keypress',checkIfEnter');
searchRef.addEventListener('click',checkIfEnter');
函数checkIfEnter(事件){
如果(event.keyCode==13 | | event.type==click'){
获取结果(查询引用值);
}
}
函数getResults(城市){
fetch(`api.base}weather?q=${city}&units=metric&APPID=${api.key}`)
.then((response)=>response.json())
.然后((天气数据)=>{
显示结果(天气数据);
});
}
功能显示结果(天气){
areaRef.innerText=`${weather.name},${weather.sys.country}`;
const dateToday=新日期();
dateRef.innerText=dateToday.toDateString();
tempRef.innerHTML=`${Math.round(weather.main.temp)}°c`;
weatherRef.innerText=weather.weather[0].main;
tempdref.innerText=`${Math.round(weather.main.temp_min)}°c/${Math.round(weather.main.temp_max)}°c`;
}