Currently, I am engaged in a small Web development project for a University course. The project involves using Express with Pug as the view engine. My goal is to iterate through an array of weather stations, creating a unique route for each one. Each route should lead to a details page where the specific value from the array is displayed.
var i = 0;
while(stations[i]!= null){
app.get('/'+stations[i]["name"], (req, res) => {
res.render("DetailsTemplate", {Station: stations[i]})
});
console.log(`detailsPageRunning on ` + '/' + stations[i]["name"] + ' '+ i);
i++;
}
block content
div(class="details")
div(class="station")
div(class="station-element")
h2 #{Station.name}
p
div(class="station-element")
h2 Weather
p #{Station.readings.at(0).weather}
div(class="station-element")
h2 Temperature
p #{Station.readings.at(0).temp} Degrees
div(class="station-element")
h2 Wind
p #{Station.readings.at(0).wind} bft.
div(class="station-element")
h2 Air Pressure
p #{Station.readings.at(0).airpressure} bpa
Currently, the routes are created as intended and the page renders fine when using the same array value for each iteration, such as Station: stations[0]
. However, the issue arises when trying to dynamically assign different values to each route using Station: stations[i]
. What adjustments need to be made for this to work properly?
I have experimented with other arrays to see if the problem lies within the array itself, but encountered the same issue.