Reputation: 49
I am having an issue either with the Jade template engine, or something else; I do not know. When I loop through an object and output it's properties it doesn't seem to output the "country" property, but outputs the "id" and "_v" properties fine. I'd expect the "sections" property not to output, because it's an object itself, but it didn't say anything in the console log.
Schema:
var CSVSchema = {
country: {type: String}
, sections: [
{
title: {type: String}
, sub_sections: [
{
title: {type: String}
, value: {type: String}
}
]
}
]
};
Search controller: This searches for a country in the mongodb database (using mongoose) and renders the Jade template passing the enclosed properties.
exports.query = function (req, res) {
var query = req.body.query;
results.find({"country": query}, function (err, data){
res.render('search/query', {
title: 'Search Query'
, query: req.body.query
, results: data
, message: req.flash('error')
})
});
}
JSON object from mongodb: This is what is stored in the data variable, passed to the Jade template via the Search Controller. For some reason the country property is appearing before the _id property, and sections isn't? I've edited the entries in mongodb to put country after _id and that didn't fix anything.
{ country: 'India',
_id: 51cda5163c7286000000000c,
sections:
[ { sub_sections: [Object],
_id: 51cda5163c72860000000011,
title: 'Undergraduate' },
{ sub_sections: [Object],
_id: 51cda5163c7286000000000d,
title: 'Postgraduate' } ],
__v: 0 }
Jade Template: To test that there wasn't an issue with Jade's rendering, I inserted a test property to see whether it outputted fine, and it did.
for result in results
- result.test = 'TESTING123'
ul
li #{result._id}
li #{result.test}
li #{result.country}
li #{result.sections}
li #{result.__v}
Output:
Package.json: I tried downgrading to Jade 0.30.x and it broke my templates, saying something on the lines of "body is not defined?".
{
"name": "test",
"description": "test",
"keywords": [
"express",
"mongoose",
"mongodb",
"passport"
],
"version": "0.0.1",
"private": true,
"engines": {
"node": "0.10.x",
"npm": "1.2.x"
},
"dependencies": {
"express": "3.3.1",
"jade": "0.31.x",
"mongoose": "3.6.13",
"connect-mongo": "0.3.2",
"connect-flash": "0.1.1",
"passport": "0.1.17",
"passport-local": "0.1.6",
"csv": "0.3.3",
"moment": "2.0.0"
},
"devDependencies": {
"mocha": "1.11.0",
"nodemon": "0.7.8"
}
}
Upvotes: 1
Views: 2359
Reputation: 49
I've resolved this issue by stringifying and parsing the object, before passing it to the Jade template.
results = JSON.parse(JSON.stringify(results))
Upvotes: 3
Reputation: 311865
The data
object you're passing into the Jade template is an array of model instances rather than plain JS objects that Jade appears to need. The cleanest way to fix that is to use a 'lean' Mongoose query instead by inserting a lean()
call into the query method chain:
results.find({"country": query}).lean().exec(function (err, data){
res.render('search/query', {
title: 'Search Query'
, query: req.body.query
, results: data
, message: req.flash('error')
})
});
This will also make your query faster.
Upvotes: 2