变量未显示在ejs模板中。 下面是模板的代码。
<%- include("./partials/header"); -%>
<h1>Formula One Weather App</h1>
<p><%= startingContent %></p>
<form action="/" method="post">
<select name="City" id="">
<option value="Melbourne">Austrialian</option>
<option value="Manama">Bahrain</option>
</select>
<button type="submit" name="button">Find Weather</button>
</form>
<p><%= main %></p>
该代码正在访问天气API,我将其打印到不需要在模板上显示的控制台。 下面是访问API的代码。
const homeStartingContent = "This is a really cool site about formula 1 weather"
const app = express();
app.set('view engine', 'ejs');
app.use(express.static("public"));
let currentweather = ""
app.use(bodyparser.urlencoded({extended: true}))
app.get("/", function(req, res){
app.post("/", function(req, res){
// console.log(req.body.City)
var city = req.body.City;
var APP_ID = "xxxxxxxxxxxxxxxxxxxxx"
// var options = {
// }
var url = `http://api.openweathermap.org/data/2.5/weather?q=${city}&appid=${APP_ID}`
request(url, function(err, response, body){
//console.log(body);
var data = JSON.parse(body)
var main = data.weather[0].main;
//var temp = data.main.temp
console.log(main)
//res.render('home', {currentweather: main});
// let cityWeather = res.write(`<p>The current weather in ${city}
// is ${main}`)
})
})
基本上,我正在尝试渲染所选城市的天气。不确定我做错了什么,将不胜感激。