首页 > 解决方案 > 在我的 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") %>

标签: javascriptnode.jsexpressejs

解决方案


推荐阅读