2017-07-30 65 views
1

我在一个小博客应用工作,我在这里停留在这个无差错猫鼬 - 转换成的ObjectId失败

{ 
    "message": "Cast to ObjectId failed for value \" 597c4ce202ca9c353fc80e8a\" at path \"_id\" for model \"Blog\"", 
    "name": "CastError", 
    "stringValue": "\" 597c4ce202ca9c353fc80e8a\"", 
    "kind": "ObjectId", 
    "value": " 597c4ce202ca9c353fc80e8a", 
    "path": "_id" 
} 

我使用findById - 下面是代码

app.get("/blogs/:id", function(req, res) { 
    Blog.findById(req.params.id, function(err, foundBlog) { 
     if (err) { 
      res.send(err); 
     } else { 
      res.render("show", { 
       blog: foundBlog 
      }); 
     } 
    }); 
}); 

这里是app.js文件

const bodyParser = require('body-parser'), 
mongoose   = require('mongoose'), 
express   = require('express'), 
app    = express(); 

//APP CONFIG 
mongoose.connect("mongodb://localhost/blog_rest"); 
app.set("view engine", "ejs"); 
app.use(express.static("public")); 
app.use(bodyParser.urlencoded({ extended: true})); 

//MONGOOS MODEL CONFIG 
var blogSchema = new mongoose.Schema({ 
    title: String, 
    image: String, 
    body: String, 
    created: { type: Date, default: Date.now} 
}) 

var Blog = mongoose.model("Blog", blogSchema); 

// Blog.create({ 
// title: "TEST BLOG", 
// image: "https://images.pexels.com/photos/457443/pexels-photo-457443.jpeg?h=350&auto=compress&cs=tinysrgb", 
// body: "First App in the series" 
// }) 

//RESTFUL ROUTES 
app.get("/", function (req,res) { 
    res.redirect("/blogs"); 
}); 

//INDEX ROUTE 
app.get("/blogs", function (req,res) { 
    Blog.find({}, function (err,blogs) { 
    if(err){ 
     // res.send("ERROR"); 
     console.log(err); 
    } 
    else { 
     res.render("index", {blogs: blogs}); 
    } 
    }) 
}); 

//NEW ROUTE 
app.get("/blogs/new", function (req,res) { 
    res.render("new"); 
}); 

//CREATE ROUTE 
app.post("/blogs", function (req,res) { 
    //Create Blog///////Blog.create takes two parameters first is the data which is in req.body.blog and second is the callback function 
    Blog.create(req.body.blog, function (err, newBlog) { 
    if (err) { 
     res.render("new"); 
    } 
    else { 
     res.redirect("/blogs"); 
    } 
    }); 
}); 

//SHOW ROUTE 
app.get("/blogs/:id", function (req,res) { 
    //String.prototype.trim(req.params.id); 
    console.log(req.params.id); 
    Blog.findById(req.params.id, function (err,foundBlog) { 
    if (err) { 
     res.send(err); 
    } else { 
     res.render("show", {blog: foundBlog}); 
    } 
    }); 
}); 




app.listen(5000, function() { 
    console.log("Server is listening on Port: 5000"); 
}); 

这里是new.ejs:

<% include ./partials/header %> 

<div class="ui main text container segment"> 
    <div class="ui huge header">New Blog</div> 
    <form class="ui form" action="/blogs" method="post"> 
     <div class="field"> 
      <label>Title</label> 
      <input type="text" name="blog[title]" placeholder="Title"> 
     </div> 
     <div class="field"> 
      <label>Image</label> 
      <input type="text" name="blog[image]" placeholder="Image"> 
     </div> 
     <div class="field"> 
      <label>Blog Content</label> 
      <textarea name="blog[body]"></textarea> 
     </div> 
     <input class="ui blue inverted button" type="submit"> 
    </form> 
</div> 

<% include ./partials/footer %> 

,这里是show.ejs

<div class="ui main text container segment"> 
    <div class="ui huge header"><%= blog.title %></div> 
</div> 

<% include ./partials/footer %> 

的console.log后,我得到了正确的ID,但它有这样的

  • '597c4ce202ca9c353fc80e8a' 在开始的空间并在URL%20每次在id之前加起来,像这样 - >http://localhost:5000/blogs/%20597c4ce202ca9c353fc80e8a,如果我从url中移除%20,那么请求的页面显示出来。 我被困在这里很长一段时间,我还没有找到解决办法。请帮忙
+0

['String.prototype.trim()'](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/trim) 。它将删除“空白”,这是您在URL中的值开始时所具有的。可能是因为您正在从中读取链接值的模板中存在错误。修复它在一个地方。 –

+0

使用str.trim()以去除空格 – Kamesh

+0

我用\t \t \t String.prototype.trim(req.params.id)修剪你的id;但是仍然会出现相同的错误 – kamal11

回答

0

感觉像ObjectId是无效的。你正在通过。 请检查:

var mongoose = require('mongoose'); 
mongoose.Types.ObjectId.isValid('your id here'); 
+0

在此之后得到相同的错误,请理解我的问题,并帮助我只是卡在这里。 – kamal11

+0

尝试findOne({_ id:'yourIdHere'}); –

+0

我已经试过了,它也给出了同样的错误 – kamal11