将请求提供给对象ID错误

时间:2016-09-21 14:04:32

标签: node.js mongodb mongoose

由于某种原因,我在尝试通过以下架构中的PUT请求更新字符串时收到以下错误:

`var mongoose = require("mongoose");

var litterSchema = new mongoose.Schema({
   name: String,
   image: String,
   description: String
});

   module.exports = mongoose.model("Litters", litterSchema);`

我很困惑因为我有一个相同的架构和路线似乎没有问题。我想知道我的某个依赖项中是否存在损坏的内容?不知道该去哪里。

这是我的路线:

router.put("/:id", middleware.adminLoggedIn, function(req, res){
    // req.body.litter on next line pulls from the square brackets on edit.ejs, this way don't have to define object here.
    Litters.findByIdAndUpdate(req.params.id, req.body.litter, function(err, updatedLitter){
        if(err){
            res.send("ERROR");
        } else {
            res.redirect("/litters/" + req.params.id);
        }
    });
});

这是我得到的错误:

{ [CastError: Cast to ObjectId failed for value "http://cdn1-www.dogtime.com/assets/uploads/2011/01/file_23204_goldendoodle.jpg" at path "_id"]
  message: 'Cast to ObjectId failed for value "http://cdn1-www.dogtime.com/assets/uploads/2011/01/file_23204_goldendoodle.jpg" at path "_id"',
  name: 'CastError',
  kind: 'ObjectId',
  value: 'http://cdn1-www.dogtime.com/assets/uploads/2011/01/file_23204_goldendoodle.jpg',
  path: '_id',
  reason: undefined }

1 个答案:

答案 0 :(得分:0)

您正在尝试使用不可能的String更新ObjectId值,并且会给出类型转换错误。

检查你的"垃圾"宾语。它有" http://cdn1-www.dogtime.com/assets/uploads/2011/01/file_23204_goldendoodle.jpg"路径上的值" _id"。