Node.js - Nested array in Jade view
Using Mongoose, I have a model Page with an embedded model of Feeds. When i go to /pages, the page.title shows up for each page, but feeds data does not. how should i modify this code to properly display the data from the feeds array? thanks a million
db.pages exmaple:
{ "title" : "testing feeds", "_id" : ObjectId("123456"), "feeds" : [
{ "0" : { "name" : "twitter", "ke开发者_开发问答y" : "1234" },
"1" : { "name" : "flickr", "key" : "5678" },
}] }
web.js
app.get('/pages.:format?', function(req, res) {
Page.find({}, function(err, pages) {
switch (req.params.format) {
case 'json':
res.send(pages.map(function(d) {
return d.toObject();
}));
break;
default:
res.render('pages/index.jade', {
locals: {
title: 'ClrTouch | Pages',
pages: pages,
feeds: pages.feed,
}
});
}
});
});
view
- each page in pages
div.page
div.pagetitle= page.title
ul
- each feed in page.feeds
li.pagefeedname= feed.name
li.pagefeedkey= feed.key
with what i have, a list is generated in the view but the list items are empty. Thanks.
Is the model that you have here what you are also testing with? If so, then the reason your list is generated but nothing is displayed is because you do not currently have values in either.
li.pagefeedname=feed.name
= "0" : { "name" : "", "key" : "" }
Try giving your feed name/keys some values and see how that plays out.
Otherwise what you have should work
tuddy, I suggest you try like this:
Page.find().lean().exec(function(err,pages){
// TODO
});
精彩评论