2013-02-06 22 views
0

我有一个Team Schema,其中包含有关团队的详细信息以及用于存储这些团队的匹配架构。我试图让匹配架构中的主/对Team对象的引用。我已经将我的代码放在下面,保存团队时出现错误,但我不禁感到自己在架构或保存匹配时做了错误。谁能帮忙?Mongoose DBrefs - 强制转换为ObjectId的值失败

到目前为止,我有以下代码:

Team.js提取

var Team = new Schema({ 
    'key' : { 
    unique : true, 
    type : Number, 
    default: getId 
    }, 
    'name' : { type : String, 
       validate : [validatePresenceOf, 'Team name is required'], 
       index : { unique : true } 
      } 
}); 

module.exports.Schema = Team; 
module.exports.Model = mongoose.model('Team', Team); 

Match.js提取

var util = require('util'); 
var mongoose = require('mongoose'); 
var Schema = mongoose.Schema; 
var Team = require('../schemas/Team').Schema; 

var Match = new Schema({ 
    'key' : { 
    unique : true, 
    type : Number, 
    default: getId 
    }, 
    'hometeam' : { 
    type : Schema.ObjectId, 
    ref : 'Team' 
    }, 
    'awayteam' : { 
    type : Schema.ObjectId, 
    ref : 'Team' 
    } 
}); 

module.exports = mongoose.model('Match', Match); 

index.js

app.get('/match', function(req, res) { 
    var key = 1356136550152; // Reading 
    Team.findByKey(key, function(err, team) { 
     if(err) { 
     res.send("An error occured"); 
     } 
     if(!team) { 
     res.send("The team does not exist"); 
     } 
     var match = new Match(); 
     match.hometeam = team; 
     match.save(function(err) { 
     if(err) { 
      util.log('Error while saving Match: ' + util.inspect(err)); 
      res.send("An error occured whilst saving the match"); 
     } else { 
      res.send("Saved the match"); 
     } 
     }); 
    }); 
    }); 

错误:

Error while saving Match: { message: 'Cast to ObjectId failed for value "{ name: \'testTeam\',\n _id: 50d500663ca6067226000001,\n __v: 0,\n key: 1356136550152 }" at path "hometeam"', 
    name: 'CastError', 
    type: 'ObjectId', 
    value: 
    [ { name: 'testTeam', 
     _id: 50d500663ca6067226000001, 
     __v: 0, 
     key: 1356136550152 } ], 
    path: 'hometeam' } 

错误与team._id

Error while saving Match: { [MongoError: E11000 duplicate key error index: testdb.matches.$team.name_1 dup key: { : null }] 
    name: 'MongoError', 
    err: 'E11000 duplicate key error index: testdb.matches.$team.name_1 dup key: { : null }', 
    code: 11000, 
    n: 0, 
    connectionId: 8, 
    ok: 1 } 

db.matches.getIndexes() 
[ 
    { 
     "v" : 1, 
     "key" : { 
      "_id" : 1 
     }, 
     "ns" : "testdb.matches", 
     "name" : "_id_" 
    }, 
    { 
     "v" : 1, 
     "key" : { 
      "key" : 1 
     }, 
     "unique" : true, 
     "ns" : "testdb.matches", 
     "name" : "key_1", 
     "background" : true, 
     "safe" : null 
    }, 
    { 
     "v" : 1, 
     "key" : { 
      "team.key" : 1 
     }, 
     "unique" : true, 
     "ns" : "testdb.matches", 
     "name" : "team.key_1", 
     "background" : true, 
     "safe" : null 
    } 
] 

回答

2

index.js它应该是:的

match.hometeam = team._id; 

代替:

match.hometeam = team; 

UPDATE

关于新的错误消息,看起来您在matches集合上有一个唯一索引,指向不存在的字段。把它放在外壳中使用:

db.matches.dropIndex('team.name_1') 
+0

你好,我试过这个,并把上面的新错误放在我原来的文章中。 – germainelol

+0

我一定在代码的其他地方做错了什么,但我看不到它 – germainelol

+0

@germainelol看起来你在'matches'上有一个杂散索引,这是造成这种情况的原因。查看更新的答案。 – JohnnyHK

相关问题