node.js - NodeJS - Mongoose document embedding -
i trying practice mongoose , node js, want use comment schema in article schema , when run server, throws error this:
invalid value schema array path
comments
here comment model
module.exports = function( mongoose ) { var schema = mongoose.schema; var commentschema = new schema({ text: string, author: string, createdate: { type: date, default: date.now } }); console.log("********"); console.log(commentschema); console.log("********"); mongoose.model( 'comment', commentschema); };
and article model:
module.exports = function(mongoose){ var schema = mongoose.schema; var comment = require("./comment"); console.log("--------"); console.log(mongoose); console.log("--------"); var articleschema = new schema({ title: string, content: string, author: string, comments: [comment.schema], createdate: { type: date, default: date.now } }); mongoose.model('article', articleschema); };
they in same folder called "models".
and app.js show bindings:
var express = require('express'); var morgan = require("morgan"); var methodoverride = require("method-override"); var utils = require("./lib/utils"); var config = require("config"); var bodyparser = require('body-parser'); var app = express(); var mongoose = require('mongoose'); var mongooseconnection = utils.connecttodatabase(mongoose, config.db); var routes = require('./routes/index'); var users = require('./routes/users'); var app = express(); // view engine setup app.set("port", process.env.port || 3000); app.use(express.static(__dirname + '/public')); app.use(morgan('dev')); app.use(bodyparser()); app.use(methodoverride()); app.set('views', __dirname + '/views'); app.set('view engine', 'jade'); app.set('view options', { layout: true}); require("./controllers/articlecontroller")(app, mongooseconnection); require("./controllers/commentcontroller")(app, mongooseconnection); require("./controllers/indexcontroller")(app, mongooseconnection); require("./models/article")(mongooseconnection); require("./models/comment")(mongooseconnection); require("./models/user")(mongooseconnection); app.listen(app.get("port"), function(){ console.log("express server listening on port" + app.get("port")); });
thanks.
at ./models/article.js
variable comment
function (you should invoking parenthesis passing mongoose variable), instead of comment model:
module.exports = function(mongoose){ // code .. var comment = require("./comment"); // code .. };
and if execute function above passing mongoose variable @ ./models/comments.js
in function, returning nothing:
module.exports = function( mongoose ) { // code .. mongoose.model( 'comment', commentschema); };
so try example created below.
comment model @ ./models/comment.js
:
module.exports = function (mongoose) { var commentschema = new mongoose.schema({ text: string, author: string, createdate: {type: date, default: date.now} }); return mongoose.model('comment', commentschema); };
article model @ ./models/article.js
:
module.exports = function (mongoose) { var comment = require('./comment')(mongoose); var articleschema = new mongoose.schema({ title: string, content: string, author: string, comments: [comment.schema], createdate: {type: date, default: date.now} }); return mongoose.model('article', articleschema); };
main file @ ./app.js
:
var mongoose = require('mongoose'); mongoose.connect('mongodb://localhost:27017/mongoose_sky'); var article = require('./models/article.js')(mongoose); var article = new article({ title: 'my article', content: 'this awesome article', author: 'wilson', comments: [ { text: 'hey article great', author: 'doug' }, { text: 'hillarious!', author: 'john' } ] }); article.save(function (err) { if (!err) { console.log('article saved'); console.log(article); } });
Comments
Post a Comment