【问题标题】:Issues with Mongoose findById commandMongoose findById 命令的问题
【发布时间】:2020-02-13 22:43:11
【问题描述】:

我有一个 nodejs express 路由器,路由如下:

router.get("/book/:id", function(req, res){

    Book.findById(req.params.id).then(function(book){
        if (book) {
            res.json(book);
        } else {
            res.send("Book Not found");
        }
    }).catch(function(err){
        if (err) {
            console.log(err);
            res.send(err);
            throw err;
        }
    })
})

当我使用邮递员测试路线时,我总是得到这个错误:

  { [CastError: Cast to ObjectId failed for value "5e441654a8b2e25bfa3d4507" at path "_id" for model "Book"]
  stringValue: '"5e441654a8b2e25bfa3d4507"',
  kind: 'ObjectId',
  value: '5e441654a8b2e25bfa3d4507',
  path: '_id',
  reason: [TypeError: hex is not a function],
  message: 'Cast to ObjectId failed for value "5e441654a8b2e25bfa3d4507" at path "_id" for model "Book"',
  name: 'CastError',
  model: Model { Book } }

findById 命令似乎请求的是 mongoose objectId 类型而不是字符串 我尝试了在线解决方案(来自堆栈和其他社区),我发现类似于:

ObjectId = mongoose.Types.ObjectId
ObjectId = mongoose.Schema.ObjectId 
ObjectId = mongoose.mongo.ObjectId 

然后 该解决方案建议执行以下操作:

id = new ObjectId(req.params.id)
Book.findById(id) 

仍然无法处理 hex is not a function 错误

有没有人遇到这个错误并设法修复它? 请注意,我正在使用:

mongodb cloud hosting, version 4
mongoose 5.8.11
nodejs 4.2.6

如果有帮助,这是我的图书模型:

const schema = new mongoose.Schema({

       title: {
           type: String,
           require: true
       },
       author: {
            type: String,
            require: true
       },
       numberPages: {
            type: Number,
            require: false
       },
       publisher: {
           type: String,
           require: false
       }

});
module.exports = mongoose.model('Book', schema);

【问题讨论】:

    标签: node.js mongoose


    【解决方案1】:

    您必须在参数中直接传递该 id 字符串,例如 id="5e441654a8b2e25bfa3d4507"。尝试不带引号传递id=5e441654a8b2e25bfa3d4507

    【讨论】:

    • 我没有在我的邮递员请求中使用任何引号localhost:4545/book/5e441654a8b2e25bfa3d4507
    • 我通过了这个 localhost:3111/book/"5e441654a8b2e25bfa3d4507"... 这给了我和你一样的错误。然后我通过这个 localhost:3111/book/5e441654a8b2e25bfa3d4507 工作
    • I 这可能是mongodb和mongoose版本的问题。
    • 你能告诉我你在后端的pamas吗?通过 console.log(req.params)
    • { id: '5e441654a8b2e25bfa3d4507' }
    【解决方案2】:

    请尝试以下方法:

    var mongoose = require('mongoose');
    ...
    var myId = new mongoose.Types.ObjectId(req.params.id)
    Book.findById(myId).then(function(book) {
    ...
    

    它应该可以工作。

    【讨论】:

    • 之前试过,报错TypeError: hex is not a function
    • 你可以试试:console.log(mongoose.Types.ObjectId.isValid(myId)) 看看你是否收到了true
    • 问题在于铸造本身 var myId = new mongoose.Types.ObjectId(req.params.id) 这是引发 hex is not a function 错误的原因,因此它会在到达 console.log 语句之前失败
    • TypeError: hex is not a function at Function.from (native) at Function.from (native) at Object.toBuffer (/home/jihedzouari/node-library-micro/books/node_modules/bson/lib/bson/parser/utils.js:20:22) at new ObjectID (/home/jihedzouari/node-library-micro/books/node_modules/bson/lib/bson/objectid.js:63:31) at /home/jihedzouari/node-library-micro/books/app/api.js:46:20 at Layer.handle [as handle_request] (/home/jihedzouari/node-library-
    • 顺便说一句,如果我添加 console.log(mongoose.Types.ObjectId.isValid(req.params.id)) 它会返回 true,这很奇怪。
    【解决方案3】:

    似乎是节点驱动程序导致了问题(发现我使用的是 4.6 的旧节点版本),我升级了我的 nodejs 版本,现在它可以工作了,谢谢大家。

    【讨论】:

      猜你喜欢
      • 2018-09-16
      • 2018-06-11
      • 1970-01-01
      • 1970-01-01
      • 2019-07-15
      • 2017-10-13
      • 2020-03-28
      • 1970-01-01
      • 2013-12-04
      相关资源
      最近更新 更多