Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何使用天气api中的输入?_Javascript_Api_Asynchronous_Weather_Openweathermap - Fatal编程技术网

Javascript 如何使用天气api中的输入?

Javascript 如何使用天气api中的输入?,javascript,api,asynchronous,weather,openweathermap,Javascript,Api,Asynchronous,Weather,Openweathermap,我认为如何解决这个异步问题。为什么在提取之前不读取输入 const key=<API-KEY> const searchInput = document.querySelector(".bottom"); const form = document.querySelector(".search-box"); let searchValue; form.addEventListener('input',updateInput);

我认为如何解决这个异步问题。为什么在提取之前不读取输入


const key=<API-KEY>
const searchInput = document.querySelector(".bottom");
const form = document.querySelector(".search-box");
let searchValue;

    

form.addEventListener('input',updateInput);
searchInput.addEventListener('submit', (e) =>{
  e.preventDefault();
  findcity(searchValue);
})


function updateInput(e){
  
  searchValue=e.target.value;
  console.log(e.target.value);
  
}


  async function findcity(query){
   
    const datafetch = await fetch(`http://api.openweathermap.org/data/2.5/weather?q=${query}+&appid=${key}`)
    .then(response =>{
      let data=  response.json()
      
    .then((data) => {
        console.log(data);
      })
    })
    }
    findcity();

常数键=
const searchInput=document.querySelector(“.bottom”);
const form=document.querySelector(“.search box”);
让我们去寻找价值;
表单.addEventListener('input',updateInput);
searchInput.addEventListener('submit',(e)=>{
e、 预防默认值();
findcity(搜索值);
})
函数更新输入(e){
搜索值=e.target.value;
console.log(如target.value);
}
异步函数findcity(查询){
const datafetch=等待获取(`http://api.openweathermap.org/data/2.5/weather?q=${query}+&appid=${key}`)
。然后(响应=>{
让data=response.json()
。然后((数据)=>{
控制台日志(数据);
})
})
}
findcity();
这里我更新了输入值,它在控制台日志中工作 当我加载页面时,console.log数据给了我immedetyle,他找不到城市