javascript - 在我的 COMPARE WEATHER 网站页面中,当用户单击比较按钮时,ejs 模板中仅呈现一个城市信息?
问题描述
'我已经检查了我的代码很多次,但我无法理解我的错误。我知道我的代码有点笨拙,并且努力让我的代码更短,每件事都工作正常。我有两个得到为 findweather 请求一个,为比较天气请求另一个。Find Weathe 我工作得很好,但
我的 app.js 文件'
const express = require("express");
const app=express();
const ejs = require("ejs");
const bodyparser = require("body-parser");
const https = require("https")
app.use(bodyparser.urlencoded({extended:true}));
app.use(express.static("public"));
app.set('view engine', 'ejs');
app.get("/compare",function(req,res){
res.render("indexcompare.ejs")
});
app.post("/compare",function(req,res){
const compare=[];
const compare1=[];
const name1=req.body.city1;
const name2=req.body.city2;
function one(name1){
const urls = "https://api.openweathermap.org/data/2.5/weather?q="+name1+"&appid=api key&units=metric"
https.get(urls,function(response){
response.on("data",function(data){
const weatherdata=JSON.parse(data);
var post = {
name:weatherdata.name ,
country:weatherdata.sys.country ,
temp:weatherdata.main.temp,
feelslike:weatherdata.main.feels_like,
tempmin:weatherdata.main.temp_min,
tempmax:weatherdata.main.temp_max,
pressure:weatherdata.main.pressure,
humidity:weatherdata.main.humidity
};
compare.push(post);
console.log(compare);
});
});
}
one(name1);
function two(name2){
const urls = "https://api.openweathermap.org/data/2.5/weather?q="+name2+"&appid=api key...&units=metric"
https.get(urls,function(response){
response.on("data",function(data){
const weatherdata=JSON.parse(data);
var post = {
name:weatherdata.name ,
country:weatherdata.sys.country ,
temp:weatherdata.main.temp,
feelslike:weatherdata.main.feels_like,
tempmin:weatherdata.main.temp_min,
tempmax:weatherdata.main.temp_max,
pressure:weatherdata.main.pressure,
humidity:weatherdata.main.humidity
};
compare1.push(post);
console.log(compare1);
res.render("compare.ejs",{compare:compare,compare1:compare1});
});
});
}
two(name2);
});
app.listen(4040,function(){
console.log("server started at 4040");
});
'我的 compare.ejs 文件'
<%- include("partials/headercompare") %>
<%for( var i = 0;i<compare.length;i++){%>
<div
style="margin-left: -161px;"" >
<h1><%= compare[i].name %></h1>
</div >
<table>
<tr>
<th><h2>Temperature</h2></th>
<th><h3><%= compare[i].temp %>°C</h3></th
</tr>
<tr>
<td>Feels Like</td>
<td><%= compare[i].feelslike %>°C</td>
</tr>
<tr>
<td>Minimum temperature</td>
<td><%= compare[i].tempmin %>°C</td>
</tr>
<tr>
<td>Maximum temperature</td>
<td><%= compare[i].tempmax %>°C</td>
</tr>
<tr>
<td>Humidity</td>
<td><%= compare[i].humidity %></td>
</tr>
<td>Pressure</td>
<td><%= compare[i].pressure %></td>
</tr>
</table>
<%}%>
<%for( var i = 0;i<compare1.length;i++){%>
<div
style="margin-left: -161px;"" >
<h1><%= compare1[i].name %></h1>
</div >
<table>
<tr>
<th><h2>Temperature</h2></th>
<th><h3><%= compare1[i].temp %>°C</h3></th
</tr>
<tr>
<td>Feels Like</td>
<td><%= compare1[i].feelslike %>°C</td>
</tr>
<tr>
<td>Minimum temperature</td>
<td><%= compare1[i].tempmin %>°C</td>
</tr>
<tr>
<td>Maximum temperature</td>
<td><%= compare1[i].tempmax %>°C</td>
</tr>
<tr>
<td>Humidity</td>
<td><%= compare1[i].humidity %></td>
</tr>
<td>Pressure</td>
<td><%= compare1[i].pressure %></td>
</table>
<%}%>
<%- include("partials/footercompare") %>
解决方案
推荐阅读
- python - Django 模型 - 在具有多个 DateTimeField 字段的模型上使用日期算术查询
- php - 如何查询帖子自定义关系字段并将解析的搜索词包含在循环中并在 WordPress 中显示结果
- tensorflow - 如何在 Tensorboard 中多次运行多个图形
- javascript - 反应钩子,将array.map的索引作为prop传递是未定义的
- java - 调用 findAll() 时发现了不止一行具有给定标识符的行,但如果我使用 findById() 遍历所有条目,则可以使用;
- r - 如何从 R 中的多栅格 grib 文件中正确提取点数据?
- javascript - 为什么 Array.from 不适用于 Qualtrics(使用 JFE)
- ios - 无法编译 React Native Share Extension iOS 12.5
- tensorflow - 如何使用词袋使训练集和测试集大小相同
- clips - CLIPS - 创建 defrule 时如何关联 2 种不同的 deftemplates 类型