【问题标题】:NodeJS - Mongoose document embeddingNodeJS - 猫鼬文档嵌入
【发布时间】:2015-09-06 00:26:48
【问题描述】:

我正在尝试练习 Mongoose 和 Node JS,我想在 Article 模式中使用 Comment 模式,当我运行服务器时,它只会抛出如下错误:

架构数组路径comments的值无效

这是我的评论模型

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);
};

还有我的文章模型:

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);
};

它们位于名为“models”的同一文件夹中。

最后是我的 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"));
});

谢谢。

【问题讨论】:

  • 我不记得需要将连接传递给模式/模型。只需摆脱这些东西并导出模型。

标签: node.js mongodb express mongoose database-schema


【解决方案1】:

在您的 ./models/Article.js 处,您的变量 Comment 是一个函数(您应该使用 mongoose 变量传递的括号来调用它),而不是 Comment 模型:

module.exports = function(mongoose){
    // some code ..

    var Comment = require("./Comment");

    // some code ..
};

即使您在函数中通过 ./models/Comments.js 处的 mongoose 变量执行上面的函数,您基本上也不会返回任何内容:

module.exports = function( mongoose ) {
    // some code ..

    mongoose.model( 'Comment', CommentSchema);
};

所以试试我在下面创建的这个例子。

./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);
};

文章模型./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);
};

主文件./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);
  }
});

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-02-25
    • 2012-05-11
    • 2012-02-15
    相关资源
    最近更新 更多