【问题标题】:Create embedded docs with Mongoose and Express使用 Mongoose 和 Express 创建嵌入式文档
【发布时间】:2013-06-29 07:25:20
【问题描述】:

绞尽脑汁想用 Mongoose 创建一个嵌入式文档。提交表单时出现以下错误:

500 CastError: Cast to undefined_method failed for value "comment goes here"

代码:

index.js

var db = require( 'mongoose' );
var Todo = db.model( 'Todo' );

exports.create = function(req, res, next){
  new Todo({
      title      : req.body.title,
      content    : req.body.content,
      comments   : req.body.comments,
  }).save(function(err, todo, count){
    if( err ) return next( err );
    res.redirect('/');
  });
};

db.js

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

// recursive embedded-document schema
var commentSchema = new Schema();

commentSchema.add({ 
    content  : String   
  , comments : [Comment]
});

var Comment = mongoose.model('Comment', commentSchema);

var todoSchema = new Schema({
    title      : { type: String, required: true }
  , content    : String
  , comments   : [Comment]
});

var Todo = mongoose.model('Todo', todoSchema);

翡翠形态

form(action="/create", method="post", accept-charset="utf-8")
  input.inputs(type="text", name="title")
  input.inputs(type="text", name="content")
  input.inputs(type="text", name="comments")
input(type="submit", value="save")

【问题讨论】:

    标签: express mongoose pug


    【解决方案1】:

    要使嵌套/嵌入文档工作,您必须使用 .push() 方法。

    var Comment = new Schema({
        content    : String
      , comments   : [Comment]
    });
    
    var Todo = new Schema({
        user_id    : String
      , title      : { type: String, required: true }
      , content    : String
      , comments   : [Comment]
      , updated_at : Date
    });
    
    mongoose.model('Todo', Todo);
    mongoose.model('Comment', Comment);
    
    exports.create = function(req, res, next){
      var todo = new Todo();
          todo.user_id    = req.cookies.user_id;
          todo.title      = req.body.title;
          todo.content    = req.body.content;
          todo.updated_at = Date.now();
          todo.comments.push({ content : req.body.comments });
    
      todo.save(function(err, todo, count){
        if( err ) return next( err );
    
        res.redirect('/');
      });
    };
    

    【讨论】:

      【解决方案2】:

      猫鼬模型从不参与模式。只有基本数据类型和猫鼬模式。不确定是否存在递归注释模式的问题,但请尝试类似的方法。

      var commentSchema = new Schema();
      
      commentSchema.add({ 
        , content  : String   
        , comments : [commentSchema]
      });
      
      var todoSchema = new Schema({
        , title      : { type: String, required: true }
        , content    : String
        , comments   : [commentSchema]
      });
      
      var Todo = mongoose.model('Todo', todoSchema);
      var Comment = mongoose.model('Comment', commentSchema);
      

      【讨论】:

      • 我刚刚尝试了您的代码并删除了架构上的第一个逗号。这是我收到的错误:Express 500 TypeError: Cannot use 'in' operator to search for '_id' in comment going here
      • 啊,是的,所以你不能只是将一组 javascript 对象粘贴到 cmets 中,你需要先将它们转换为 mongoose Comment 模型实例,然后使用嵌入的 cmets 保存 Todo。跨度>
      猜你喜欢
      • 2017-06-22
      • 2012-02-23
      • 2016-09-28
      • 2018-03-30
      • 2014-01-29
      • 1970-01-01
      • 2011-12-21
      • 2014-07-13
      • 1970-01-01
      相关资源
      最近更新 更多