簡體   English   中英

如何將信息從一個 function 傳遞到另一個

[英]How can I pass information from one function to another

我正在使用節點和 Ejs 使用天氣應用程序。 最初,輸入城市后,信息會顯示在同一頁面上,但我試圖讓它顯示在名為結果的新頁面上。 我已經知道新頁面將在 console.log 中加載而沒有任何錯誤,但沒有出現任何 API 信息。 它只是我在結果文件中的 HTML。

JS:

const express = require("express");
const https = require('https');
const ejs = require("ejs");
const bodyParser = require("body-parser");
const date = require(__dirname + "/result.js");

const app = express();

var city = {};
let weatherData = [];

app.use(bodyParser.urlencoded({
  extended: true
}));

app.set('view engine', 'ejs');
app.use(express.static("public"));

app.get("/", function(req, res) {

  res.sendFile(__dirname + "/index.html");
});

app.get("/index", function(req, res) {
  res.render("index");
});

app.post("/", function(req, res) {

  const query = req.body.cityName;
  const apiKey = "d56f0e3ec353b519c8e1fcfff89a4326";
  const units = "metric";
  const url = "https://api.openweathermap.org/data/2.5/weather?q=" + query + "&appid=" + apiKey + "&units=" + units;

  https.get(url, function(response) {
    console.log(response.statusCode);

    response.on("data", function(data) {
      const weatherData = JSON.parse(data)
      const temp = weatherData.main.temp
      const feels_like = weatherData.main.feels_like
      const description = weatherData.weather[0].description
      const icon = weatherData.weather[0].icon
      const clouds = weatherData.clouds.all
      const imageURL = "http://openweathermap.org/img/wn/" + icon + "@2x.png"
      const cloudsImageURL = "http://openweathermap.org/img/wn/03n@2x.png"

      console.log(weatherData);

    });

  });

  var city = query;
  res.redirect("/result");

})

const content1 = "11Scelerisque eleifend donec pretium vulputate sapien. Rhoncus urna neque viverra justo nec ultrices. Arcu dui vivamus arcu felis bibendum. Consectetur adipiscing elit duis tristique. Risus viverra adipiscing at in tellus integer feugiat. Sapien nec sagittis aliquam malesuada bibendum arcu vitae. Consequat interdum varius sit amet mattis. Iaculis nunc"

app.get("/result", function(req, res) {
  res.render('result', {
    content: content1,
    description: weatherData.description,
    city: weatherData.city,
    temp: weatherData.temp,
    feelsLike: weatherData.feels_like

  });

});

app.listen(3300, function() {
  console.log("Server is running on port 3300.");
});

EJS(結果頁面)

<html lang="en" dir="ltr">
  <head>
    <meta charset="utf-8">
    <title>Weather App</title>
    
    <link href="/css/style.css" rel="stylesheet" type="text/css" />
    <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@300;400;900&display=swap" rel="stylesheet">
  </head>
  <body>
  
    <div id="wrapper" class="result-pg">
        
        <h1>The weather is currently <span class="description"><%= description %></span></h4>
        <img class="weather-img" src="">
        <h1>The temperature in <span class="city"><%= city %></span> is <span class="temp"><%= temp %></span> degrees. It feels more like <span class="feels-like"><%= feelsLike %></span></h1>
        
        <p><%= content %></p>
          
    </div>

  </body>
</html>

任何幫助,將不勝感激。

您正在為來自 API 的響應設置一個局部變量weatherData 您需要將它放在/result function 中使用的全局變量中。 如此改變

const weatherData = JSON.parse(data)

weatherData = JSON.parse(data)

我不確定為什么要設置所有其他變量,例如tempfeels_like ,因為您從不使用它們。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM