【问题标题】:Mongoose DBrefs - Cast to ObjectId failed for valueMongoose DBrefs - 转换为 ObjectId 的值失败
【发布时间】:2013-02-07 22:26:28
【问题描述】:

我有一个包含团队详细信息的团队架构,以及一个用于存储这些团队的比赛架构。我正在努力使比赛架构中的主/客队成为对 Team 对象的引用。我把我的代码放在下面,保存团队时出现错误,但我不禁觉得我在模式或保存比赛时做错了。有人可以帮忙吗?

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

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
    }
]

【问题讨论】:

    标签: javascript mongodb mongoose


    【解决方案1】:

    index.js 中应该是:

    match.hometeam = team._id;
    

    代替:

    match.hometeam = team;
    

    更新

    关于新的错误消息,您似乎在 matches 集合上有一个唯一索引,该索引引用了不存在的字段。使用以下命令将其放入 shell:

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

    【讨论】:

    • 您好,我已经尝试过了,并将上面的新错误放在我原来的帖子中。
    • 我一定在代码的其他地方做错了,但我看不到它
    • @germainelol 看起来你在matches 上有一个杂散索引,这是造成这种情况的原因。查看更新的答案。
    • { "nIndexesWas" : 6, "ok" : 1 } 你觉得这行得通吗?因为我仍然遇到同样的错误
    • @germainelol 是的,这意味着它已成功删除。您可以通过db.matches.getIndexes()查看仍然存在的索引。
    猜你喜欢
    • 2013-03-24
    • 2021-09-05
    • 2021-03-09
    • 2018-01-06
    • 1970-01-01
    • 2021-03-14
    • 2020-04-18
    • 2020-12-16
    • 2022-08-15
    相关资源
    最近更新 更多