[英]NodeJS - Mongoose document embedding
I am trying to practice Mongoose and Node JS, I want to use Comment schema in Article schema and when I run the server, it just throws an error like this: 我正在尝试练习Mongoose和Node JS,我想在Article模式中使用Comment模式,当我运行服务器时,它只会引发如下错误:
Invalid value for schema Array path
comments
架构数组路径comments
值无效
Here is my 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 my 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 are in the same folder called "models". 它们位于称为“模型”的同一文件夹中。
And finally my app.js to show the bindings: 最后是我的app.js显示绑定:
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 your ./models/Article.js
your variable Comment
is a function (you should be invoking it with parenthesis passing by the mongoose variable), instead of the Comment model: 在您的./models/Article.js
您的变量Comment
是一个函数(您应该通过带猫鼬变量的圆括号来调用它),而不是Comment模型:
module.exports = function(mongoose){
// some code ..
var Comment = require("./Comment");
// some code ..
};
And even if you execute your function above passing by the mongoose variable at your ./models/Comments.js
in your function, you are basically returning nothing: 即使您在函数上方通过./models/Comments.js
变量传递函数来执行函数,您基本上也不会返回任何内容:
module.exports = function( mongoose ) {
// some code ..
mongoose.model( 'Comment', CommentSchema);
};
So try this example that I created below. 因此,请尝试下面创建的这个示例。
Comment Model at ./models/Comment.js
: ./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 at ./models/Article.js
: ./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 at ./app.js
: 位于./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 is my awesome article',
author: 'wilson',
comments: [
{
text: 'hey your article is great',
author: 'doug'
},
{
text: 'hillarious!',
author: 'john'
}
]
});
article.save(function (err) {
if (!err) {
console.log('article was saved');
console.log(article);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.