node.js - How to display a Mongo field on Nodejs with Mongoose and Jade? -
currently see displaying entire document, not 1 field. how display 1 field?
i have tried following i'm getting "undefined"s don't see displayed.
way 1 main js:
// app.js var schema = new mongoose.schema({ a_field : string }, { collection : 'the_col_with_data' }); var documentmodel = mongoose.model('doc', schema); var a_document = documentmodel.findone({}, callback); app.get('/', function(req, res) { res.render('index', { pass_var: a_document.a_field }); });
way 1 view:
// index.jade #{ pass_var }
way 2 main js:
// app.js var schema = new mongoose.schema({ a_field : string }, { collection : 'the_col_with_data' }); var documentmodel = mongoose.model('doc', schema); var a_document = documentmodel.findone({}, callback); app.get('/', function(req, res) { res.render('index', { pass_var: a_document }); });
way 2 view:
// index.jade #{ pass_var.a_field }
interaction mongoose asynchronous. means, can't rely on return value of mongoose operation. have perform logic operates on document within callbacks. in case, a_document
doesn't point document you're trying find. instead, have use document within callback:
app.get('/', function(req, res) { documentmodel.findone({}, function(err, doc) { if(err) { res.send(500); return; } res.render('index', {pass_var: doc}); }); });
Comments
Post a Comment