gpt4 book ai didi

调用另一个 API 后 JavaScript API 返回未定义状态

转载 作者:行者123 更新时间:2023-12-01 02:34:39 26 4
gpt4 key购买 nike

//app.js
const SEARCH_VIEW = document.getElementById('search_view');
const RESULTS_VIEW = document.getElementById('results_view');
const FORECAST_VIEW = document.getElementById('forecast_view')


function loadCities(){


const cities = ["London", "Paris", "Madrid", "Lisbon","Ohrid"];

var options = null;

var dest = document.getElementById('dest');

//Looping the cities
cities.forEach(city => {
options += '<option>' + city +'</options>';
});

dest.innerHTML = options;
}

function gettingWeather(){

// 1. Open the Url
var dest = document.getElementById('dest').value;
var url = ('http://api.openweathermap.org/data/2.5/weather?q='+ dest + '&appid=exampleAppId');
console.log(url);
console.log(dest);

// 2. Fetch the URL

fetch(url)
.then(response => {
if(response.status !== 200){
console.error("API failed, Status Code " + response.status);
return;
}
console.log(response);
// 3.We make the response .json and open the data

response.json().then(data => {
console.log(data);
RESULTS_VIEW.style.visibility = 'visible';
// Temperature
document.getElementById('Temperature').textContent = data.main.temp;

//Wind

document.querySelector('.Wind').textContent = data.wind.speed * data.wind.deg;
//Description
document.querySelector('.Description').textContent = data.weather[0].description;
});

}).catch(err => {
console.error("Fetch error "+ err);
});
}

function forecast(){
const API_BASE = 'http://api.openweathermap.org/data/2.5/forecast?mode=json&';
const API_KEY = 'appid=exampleAppId&';
var dest = document.getElementById('dest').value;
var url = API_BASE + API_KEY + 'q=' + dest.value;
console.log(url);
console.log(dest.value);

// 2. Fetch the URL

fetch(url)
.then(response => {
if(response.status !== 200){
console.error("API failed, Status Code " + response.status);
return;
}
console.log(response);
// 3.We make the response .json and open the data

response.json().then(data => {
console.log(data);

RESULTS_VIEW.style.visibility = 'hidden';
FORECAST_VIEW.style.visibility= 'visible';



});

}).catch(err => {
console.error("Fetch error "+ err);
});
}

index.html

<!DOCTYPE html>
<html>
<head>
<title>Weather App</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width ,initial-scale=1">
<link href="styles.css" type="text/css" rel="stylesheet">

</head>
<body onload="loadCities();">







<div class="container">

<div id = "results_view">
<div class="inputWrapper">
<h1> Weather App </h1>
<p>Choose a city.</p>
<select id="dest" onchange="gettingWeather();" width=150 style="width:150px" ></select><br>
<label>Temperature</label>
<label class="Temperature" id="Temperature"></label><br>
<label>Wind</label>
<label class="Wind" id="Wind"></label><br>
<label>Description</label>
<h1 class="Description" id="Description"></h1>

<button onclick="forecast();">Forecast</button>
</div><!-- InputWrapper -->
</div>
<div id="forecast_view">
<h1>ForeCast</h1>
</div>

</div> <!-- end container -->




<script src="app.js"></script>
</body>
</html>

我的任务是随机选择 5 个欧洲城市并显示一些属性,但单击按钮时我需要显示该城市的预测。我尝试使用词法作用域,我的应用程序的一半工作在简单的部分,其中我显示从 API 获取的一些属性,当我单击按钮 预测 时,我的响应有错误 Error 但错误是也是我选择的城市。如果我使用任何城市,则预测中的值是未定义的。我不确定为什么会出现此错误,并且我没有尝试过任何内部关闭如果您不知道任何答案,我什至会感激对类似工作的见解或引用。

谢谢

最佳答案

问题是您的变量“dest”已经包含该值。因此,“dest”中包含您所在城市的名称,并且使用未定义的 dest.value,因为 dest 是一个字符串并且没有名为 value 的属性。要修复您的代码,只需删除网址中的 .value:

const API_BASE = 'http://api.openweathermap.org/data/2.5/forecast?mode=json&';
const API_KEY = 'appid=6c345b20d0c8fac21a36761eb7d6cd38&';
var dest = document.getElementById('dest').value;
var url = API_BASE + API_KEY + 'q=' + dest; // only dest NOT dest.value
console.log(dest); // the city selected

关于调用另一个 API 后 JavaScript API 返回未定义状态,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48027663/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com