Reputation: 791
I have an array I need that I need to pass to express, render and then iterate through on the Jade side to display all the results, however the below function does not work. It only displays one piece of data, five times from the array. The expected behaviour is that it would loop through the array and display one of each object in the template.
Is the syntax for the jade and express correct? The documentation is quite light.
Express
router.get('/', function(req, res, next){
weekendPlans(function(err, theWeekend) {
if (err) throw err
console.log(theWeekend);
res.render('index', {theWeekend : theWeekend});
});
Jade
h2 On This Weekend
.scroll
each result, i in theWeekend
div.title
#[a(target="_blank" href="#{theWeekend.url}") #{theWeekend.title}]
p WHEN: #{theWeekend.selectedDate}
Data
{ _id: 56fe9fe71f84acc2564b9fe8,
url: 'http://www.timeoutshanghai.com/features/Blog-Food__Drink/35271/Baristas-showcase-latte-art-in-Shanghai.html',
title: 'TIMEOUT',
selectedDate: Sat Apr 02 2016 01:00:00 GMT+0100 (BST),
__v: 0 }
Upvotes: 0
Views: 1028
Reputation: 1707
In your Jade file, you are iterating over the data object, which loops once for each key-value pair in your object. If you only want one entry for the data object, try this instead:
div.title
#[a(target="_blank" href="#{theWeekend.url}") #{theWeekend.title}]
p WHEN: #{theWeekend.selectedDate}
it should work.
Upvotes: 1
Reputation: 642
What array? All I can see is an object. You are looping through one object. And for every key in the object you make jade print out a div with an anchor and a paragraph. Since your object holds five keys, you get five divs.
That is why when you wrote result.key
it gave you nothing, because that does not exist. In that scenario result[1]
would have given you the url. And when you wrote theWeekend.key
it gave you the same text every time.
If you only have one object you don't need a loop. You should write:
.scroll
div.title
#[a(target="_blank" href="#{theWeekend.url}") #{theWeekend.title}]
p WHEN: #{theWeekend.selectedDate}
Upvotes: 1