【问题标题】:Mongoose findById CastError: Cast to ObjectId failed for value at path "_id"Mongoose findById CastError:转换到 ObjectId 路径“_id”处的值失败
【发布时间】:2015-07-16 01:38:23
【问题描述】:

我有一个简单的应用程序,我正在使用 mongoose 在 mongodb 上保存数据

我也在使用autoIncrement 来创建 mongodb 自动增量 ID。这是我的代码:

var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');

        var connection = mongoose.connect('mongodb://localhost/mymusic', function(err) {
  if(err) {
      console.log('connection error', err);
  } else {
    console.log('connection successful');
    }
});
var Schema = mongoose.Schema;
autoIncrement.initialize(connection);

var singerSchema = new Schema({
          artist_name:  { type : String ,index: true, unique : true },
          artist_id:  { type : String },
          poster:  { type : String },
          created: { type: Date, default: Date.now },
          path: { type : String }
        });
singerSchema.plugin(autoIncrement.plugin, 'singer');
        var singer = mongoose.model('singer', singerSchema);

     var akon = new singer({
      artist_name:'Akon',
      artist_id:150,
      poster:'akon.jpg',
      path: 'akon'
     });
     // Save Singers
     akon.save(function(err){
        if(err)
        console.log(err);
        else
        console.log(fed);
     });

现在当我想通过 _id 查询时:

    singer.find({ _id: 608 }, function(err, singer) {
      if (err) throw err;

      // show the one user
      console.log(singer);
    });

我得到这个错误:

CastError: Cast to ObjectId failed for value "608" at path "_id"
    at ObjectId.cast (/home/app/node_modules/mongoose/lib/schema/objectid.js:132:13)
    at ObjectId.castForQuery (/home/app/node_modules/mongoose/lib/schema/objectid.js:182:17)
    at module.exports (/home/app/node_modules/mongoose/lib/cast.js:202:32)
    at Query.cast (/home/app/node_modules/mongoose/lib/query.js:2334:10)
    at Query.find (/home/app/node_modules/mongoose/lib/query.js:995:10)
    at Function.find (/home/app/node_modules/mongoose/lib/model.js:1023:13)
    at Object.<anonymous> (/home/app/search.js:22:8)
    at Module._compile (module.js:456:26)
    at Object.Module._extensions..js (module.js:474:10)
    at Module.load (module.js:356:32)

其他字段的查询工作正常,我的问题只是 _id 字段。

【问题讨论】:

  • 问题是 autoIncrement 插件,我不知道为什么,但是使用常规的 mongodb _id 可以正常工作。

标签: mongodb mongoose


【解决方案1】:

_id"608"似乎没有singer数据,你可以试试数据库中存在的一些数据。这个怎么样?

singer.find({ _id: 1 }, function(err, singer) {
  if (err) throw err;

  // show the one user
  console.log(singer);
});

当我使用错误的 _id 查询时遇到了同样的问题。

CastError: Cast to ObjectId failed for value "55263f72fa91f165119390" at path "_id"
at ObjectId.cast (/Users/Ethan-Wu/repository/github/jackfruit-server/node_modules/mongoose/lib/schema/objectid.js:117:13)
at ObjectId.castForQuery (/Users/Ethan-Wu/repository/github/jackfruit-server/node_modules/mongoose/lib/schema/objectid.js:166:17)
at Query.cast (/Users/Ethan-Wu/repository/github/jackfruit-server/node_modules/mongoose/lib/query.js:2340:32)
at Query.findOne (/Users/Ethan-Wu/repository/github/jackfruit-server/node_modules/mongoose/lib/query.js:1118:10)

希望对你有所帮助。

【讨论】:

  • 没有歌手数据,我在终端的 mongodb 及其工作中进行了测试。正如我所说的,我唯一一次使用 autoIncrement 插件时它不起作用。
猜你喜欢
  • 2017-05-26
  • 2014-06-20
  • 2016-11-11
  • 2017-03-26
  • 2020-05-20
  • 2019-01-06
  • 2016-05-06
  • 2018-03-17
  • 2021-06-05
相关资源
最近更新 更多