I'm new to express and pug so forgive me if this is a noob question but how do I pass the array I created in one function to the route that will render the pug template. I ultimately want to loop though the array and render it as a table.
My code - first I create the array from the data received from a file with the lodash map method. *EDIT - added full code structure -
fs.readFile(file, 'utf8', function (err, data) {
if (err) {
console.log('Error: ' + err);
return;
}
var data = JSON.parse(data);
var newEventList = data.events.map(events => ({
id: events.id ,
name: events.name ,
venue: events.place.name ,
address: events.place.location.street + " " +
events.place.location.city + " " + events.place.location.zip ,
coverPicture: events.coverPicture ,
description: events.description ,
startTime: events.startTime ,
endTime: events.endTime
}));
});
So now I have an array of objects called newEventList.
My route for pug is
app.get('/', function (req, res, newEventList) {
res.render('index', { title: 'Hey', message: 'Hello there!',
newEventList
})
})
I added title and message to test my pug template. in my Pug Template I have
h1= message
p= newEventList
but in the paragraph that is rendered I get a long error message -
function next(err) { // signal to exit route if (err && err === 'route') { return done(); } // signal to exit router if (err && err === 'router') { return done(err) } var layer = stack[idx++]; if (!layer) { return done(err); } if (layer.method && layer.method !== method) { return next(err); } if (err) { layer.handle_error(err, req, res, next); } else { layer.handle_request(req, res, next); } }
What am I doing wrong please?
From your route definition's callback, remove or rename newEventList
as your third param.
In your example, newEventList
is getting assigned to the done/next
callback. Hence you're seeing a function being printed in your paragraph.
var newEventList = // Whatever value;
app.get('/', function (req, res) {
res.render('index', { title: 'Hey', message: 'Hello there!',
newEventList
})
})
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.