This seems to work.....for now
each synthObject in docs
each item in synthObject.synths
li #{item.synth_name} #{item.xpos} #{item.ypos}
题
When I loop through my object everything works and I can render all instances of it to the page. The problem is that I want to be able to select a specific object key whereby every object that has that key renders its corresponding value to the page.
My code is below with a more clear explanation
Mongoose.js schema
var synthSchema = mongoose.Schema({
patchName: String,
synths: Object,
});
var SynthObject = mongoose.model('Synth', synthSchema);
Node.js / Express code
var synthPatch = new SynthObject({})
synthPatch.synths = [{
synth_name: "blah blah",
xpos: 12,
ypos: 23
},
{
synth_name: "more blah blah",
xpos: 02,
ypos: 238
},
]
synthPatch.save(function (err, ok) {
if (err) return console.error(err);
});
app.get('/returnedData', function(req, res){
SynthObject.find({}, function (err, docs) {
res.render('returnedData', {
title: 'Tasks index view',
docs: docs
});
});
});
JADE
table.table
each synth in docs
tr
td #{synth}
ul
The above code outputs ( from MongoDB)
{ synths: [
{ synth_name: 'blah blah', xpos: 12, ypos: 23 },
{ synth_name: 'more blah blah', xpos: 2, ypos: 238 }
],
_id: 5352c4c46f5127d40e7ba8ec, __v: 0
}
Ok so far so good. The above code is just what I wanted, so lets loop through it and get keys and values
JADE code
each synthObject in docs
tr
each value, key in synthObject.synths
each v, k in value
td #{k} #{v}
Perfect...this outputs the following
ypos 23 xpos 12 synth_name 'blah blah'
ypos 238 xpos 2 synth_name 'more blah blah'
However, now I run into my problem which is I want to select something like all of the synth_name from every object and output all of the synth_name values. I've tried a bunch of things but can't get it to work.
解决方案 2
This seems to work.....for now
each synthObject in docs
each item in synthObject.synths
li #{item.synth_name} #{item.xpos} #{item.ypos}
其他提示
I've tested it on: http://jade-lang.com/demo/
It works? Only difference i did was using 'docs' instead of 'synths' of given mongo data.
Jade Input
table.table
each synth in docs
tr
td #{synth.synth_name}
Locals
{ docs: [
{ synth_name: 'blah blah', xpos: 12, ypos: 23 },
{ synth_name: 'more blah blah', xpos: 2, ypos: 238 }
],
_id: "5352c4c46f5127d40e7ba8ec", __v: 0
}
results in:
<table class="table">
<tr></tr>
<td>blah blah</td>
<tr></tr>
<td>more blah blah</td>
</table>