populating associations in mongodb -
i'm new mean , mongodb , trying figure out how populate answers questions. changed words in platform i'm learning (examples posts , comments).
here's error message i'm getting.
/users/joerigby/documents/codingdojo/full mean/blacktest3/node_modules/express/lib/view.js:62 throw new error('no default engine specified , no extension prov ^ error: no default engine specified , no extension provided. @ new view (/users/joerigby/documents/codingdojo/full mean/blacktest3/node_modules/express/lib/view.js:62:11) @ eventemitter.render (/users/joerigby/documents/codingdojo/full mean/blacktest3/node_modules/express/lib/application.js:569:12) @ serverresponse.render (/users/joerigby/documents/codingdojo/full mean/blacktest3/node_modules/express/lib/response.js:961:7) @ /users/joerigby/documents/codingdojo/full mean/blacktest3/server/controllers/answers_c.js:53:12 @ query.<anonymous> (/users/joerigby/documents/codingdojo/full mean/blacktest3/node_modules/mongoose/lib/query.js:2112:28) @ /users/joerigby/documents/codingdojo/full mean/blacktest3/node_modules/mongoose/node_modules/kareem/index.js:177:19 @ /users/joerigby/documents/codingdojo/full mean/blacktest3/node_modules/mongoose/node_modules/kareem/index.js:109:16 @ process._tickcallback (node.js:355:11) 24 aug 16:48:22 - [nodemon] app crashed - waiting file changes before starting...
below codes..
from answers_controller.js on server side
show: function(req, res){ console.log(req.params.id); question.findone({_id: req.params.id}) .populate('answers') .exec(function(err, question) { res.render('question', {question: question}); }); } }
from answer model
var mongoose = require('mongoose'); var schema = mongoose.schema; var objectid = require('mongodb').objectid; var answerschema = new mongoose.schema({ answer: string, description: string, likes: {type: number, default: 0}, created_at: {type: date, default: date.now }, _question: {type: schema.objectid, ref: 'question'} }); mongoose.model('answer', answerschema);
from question model
var mongoose = require('mongoose'); var schema = mongoose.schema; var objectid = require('mongodb').objectid; var questionschema = new mongoose.schema({ title: string, description: string, created_at: {type: date, default: date.now }, answers: [{type: schema.types.objectid, ref: 'answer'}] }); mongoose.model('question', questionschema);
let me know if need else at. time.
the error not mongo or mongoose. check stack trace in error message. problem res.render
. in order use method must either configure express have default view engine (ejs, jade, etc.) or provide file extension after 'question' knows template file in order render html.
// in main server file // set default view engine app.set('view engine', 'ejs'); // set view folder (where looks template files app.set('views', pathtoviewfolder);
now can create question.ejs
file in folder @ pathtoviewfolder
.
Comments
Post a Comment