10

与. Mongoose.js_node.js

我有这个架构:

var Photo = new Schema({
     URL:String
    ,description:String
    ,created_by:{type:ObjectId, ref:'User'}
    ,created_at:{type:Date, default:Date.now()}
});

var User = new Schema({
    name:{type:String,index:true}
    ,email:{type:String,index:true, unique:true}
});

//Task model                                                                                                                                                                                       
var Task = new Schema({
    title:String
   ,created_by:{type:ObjectId, ref: 'User'}
   ,created:{type:Date, default:Date.now()}
   ,responses:[{
       type:Number
      ,user:{type:ObjectId, ref: 'User'}
       ,comment:String
       ,avatarURL:String
       ,photo:{type:ObjectId, ref: 'Photo'}
      ,created:{type:Date, default:Date.now()}
   }]
});

//Group model                                                                                                                                                                                      
var Group = new Schema({
     name:String
    ,tasks:[Task]
});

并且此代码出错(组很好,idx 处的任务很好,响应是一个空数组,用户有效,照片有效):

var typePhoto = 6;
 var resp = {
      type: typePhoto//photo                                                                                                                                                 
      ,user: user._id
      ,photo: photo._id
 };


 group.tasks[taskIdx].responses.push(resp); //errors out here

那时我收到以下错误:

/home/admin/notitws/node_modules/mongoose/lib/utils.js:434
        throw err;
              ^
CastError: Cast to number failed for value "[object Object]" at path "undefined"
    at SchemaNumber.cast (/home/admin/notitws/node_modules/mongoose/lib/schema/number.js:127:9)
    at Array.MongooseArray._cast (/home/admin/notitws/node_modules/mongoose/lib/types/array.js:78:15)
    at Object.map (native)
    at Array.MongooseArray.push (/home/admin/notitws/node_modules/mongoose/lib/types/array.js:187:23)
    at exports.taskAddPhoto (/home/admin/notitws/routes/group.js:1097:35)
    at Promise.exports.createPhoto (/home/admin/notitws/routes/photos.js:106:4)
    at Promise.addBack (/home/admin/notitws/node_modules/mongoose/lib/promise.js:128:8)
    at Promise.EventEmitter.emit (events.js:96:17)
    at Promise.emit (/home/admin/notitws/node_modules/mongoose/lib/promise.js:66:38)
    at Promise.complete (/home/admin/notitws/node_modules/mongoose/lib/promise.js:77:20)

有关如何解决此问题或可能导致此问题的任何想法?

PS不知道这是否重要,但在我正在填充tasks.responses.user和获取组的呼叫tasks.responses.phototasks.created_by

4

2 回答 2

26

mongoose 使用“type”关键字来确定字段的类型。Mongoose 可能认为响应的类型是 Number 而不是数组。

尝试:

responses:[{
   type: {type: Number}
   ,user:{type:ObjectId, ref: 'User'}
   ,comment:String
   ,avatarURL:String
   ,photo:{type:ObjectId, ref: 'Photo'}
   ,created:{type:Date, default:Date.now()}
}]

另一种选择是将您的响应对象包装到一个模式中:

responses: [Response]
于 2013-02-21T14:44:25.977 回答
2

我正在发布将我的转换固定为 objectId 的内容。这是我的 features 文档的 updatedBy key 的故事。我添加了一些评论,向您展示有问题的区域对应于铸造问题。

进口猫鼬;

    var mongoose = require('mongoose');

定义对象ID;

    var ObjectId = mongoose.Schema.Types.ObjectId;

在你的模式中定义类型;

    var featureSchema = new Schema({
        name: String,
        isSystem: { type: Number, min: 0, max: 1 },
        updatedBy: {type:ObjectId, ref:'users'}, //here I defined the field!
        updatedAt: { type: Date, default: Date.now }
});

然后通过 post 方法插入文档;

    app.post('/features', function (req, res){
          var feature;
          console.log("POST: ");
          console.log(req.body);
          feature = new Feature({
                   name: req.body.name,
                   isSystem: req.body.isSystem,
                   updatedBy: req.body.updatedBy, //here I insert the objectId field
                   updatedAt: new Date()
          });
          feature.save(function (err) {
                   if (!err) {
                            return console.log("created");
                   } else {
                        return console.log(err);
                   }
          });
         return res.json({ features: feature });
    });

最后这是你的 json 字符串。您可以从 google chrome javascript 控制台或类似控制台启动它;

    jQuery.post("/features", {
                "name": "Receipt",
                "isSystem": 0,
                "updatedBy": "528f3d44afcb60d90a000001" 
     },function (data, textStatus, jqXHR) {
                 console.log("Post resposne:"); console.dir(data);                 
                 console.log(textStatus);                             
                 console.dir(jqXHR);
     });
于 2013-11-22T18:49:51.180 回答