Reputation: 25
I am a bit confused about how to best handle data from one source to another through Express. To make this as short as possible, I'll just write a quick example of my "problem". I've tried reading up on this but since this incorporates so many parts I have got "lost in searching" on the way so I figured I (might) be better off just asking on SO.
Anyway, let's say I have a MongoDB document like this:
{
"_id" : ObjectId("xxxYYY"),
"cheese" : [
{
"value" : "true",
"key" : "bleu",
"table" : "cheeselist"
},
{
"value" : "true",
"key" : "gouda",
"table" : "cheeselist"
}
]
}
I "send" this through my controller, where cheese from above is set as an array in my Mongo model:
Cheese.find(function (err, docs) {
var CheeseChunks = [];
var chunkSize = 3;
for (var i = 0; i < docs.length; i += chunkSize) {
CheeseChunks.push(docs.slice(i, i + chunkSize));
}
res.render('api/index', {
url: req.url,
cheeses: CheeseChunks
});
})
And finally, I use it in my Jade template:
each cheeseRow in cheeses
.row: each cheese in cheeseRow
.col-md-4
each key in cheese.cheese
p= JSON.stringify(key)
As you can see, right now the only way for me to see the values is to stringify the key, but then I get the whole string and not just the keys value. I am getting confused mostly because I think that I have made a mistake earlier in the workflow which makes this more complicated that it ought to be. Am I right or is there an easy way to solve it in the Jade-template? Or should I perhaps save the string as something other than an array in MongoDB etc.?
Thanks in advance!
Upvotes: 0
Views: 44
Reputation: 20633
This should give your desired output:
Jade
each cheeseRow in cheeses
.row: each cheese in cheeseRow.cheese
.col-md-4
p= cheese.value
.col-md-4
p= cheese.key
.col-md-4
p= cheese.table
Output:
<div class="row">
<div class="col-md-4">
<p>true</p>
</div>
<div class="col-md-4">
<p>bleu</p>
</div>
<div class="col-md-4">
<p>cheeselist</p>
</div>
<div class="col-md-4">
<p>true</p>
</div>
<div class="col-md-4">
<p>gouda</p>
</div>
<div class="col-md-4">
<p>cheeselist</p>
</div>
</div>
Upvotes: 0