arrays - Use result from MongoDB in Javascript file -
i trying pass results mongodb query use in javascript file in view. using handlebars view engine. mongodb schema is:
var mongoose = require('mongoose'); var schema = mongoose.schema; var schema = new schema({ name: {type: string, required: true}, address: {type: string, required: true}, lat: {type: string}, lng: {type: string}, classes: [{ day: string, start: string, end: string }] }); module.exports = mongoose.model('dojo', schema);
my routing file has following:
router.get('/classes', function(req, res, next) { dojo.find(function(err, dojos) { if (err) { console.error(err); } res.render('admin/classes', { layout: 'layout-admin.hbs', title: 'admin', js: 'classes-admin', dojos: dojos }); }); });
within hbs file, have:
<p>{{ dojos }}</p>
the code above shows variable on screen ok when try console.log within script tags uncaught syntaxerror. code is:
<script type="text/javascript"> console.log({{ dojos }}); </script>
any advice on i'm going wrong great appreciated.
thanks matt
try change script tag:
<script type="text/javascript"> var mongo_result = {{dojos}}; console.log({{ mongo_result }}); </script>
Comments
Post a Comment