I am working on a small Web-development project, express with pug as the view-engine, for my University-course. I wish to iterate through an array of Weather-stations, creating a route for every one, with a details-page where the value of 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 Wetter
p #{Station.readings.at(0).wetter}
div(class="station-element")
h2 Temperatur
p #{Station.readings.at(0).temp} Grad
div(class="station-element")
h2 Wind
p #{Station.readings.at(0).wind} bft.
div(class="station-element")
h2 Luftdruck
p #{Station.readings.at(0).luftdruck} bpa
Now the routes are created just as i want them to, and the page is able to render fine, if i give it, through every iteration, the same array. For example Station: stations[0]
works fine. It is just kinda useless if i cant have different values on every route. Why does Station: stations[i]
not work? What would i have to change for it to work?
I have tried using other arrays, to see weather the problem was with the array, but those showed the same problem.
2
Answers
Your code has set the basis, the code below code clarifies certain points in it. Please see if it is useful. The code employs an array of route paths. There is another method using a route parameter, posted separately.
Testing a valid request:
Testing an invalid request:
The code below shows addressing the requirement using a route parameter. The other post uses an array of route paths.
Testing a valid route parameter
Testing an invalid route parameter