Expressjs路由问题

我想在我的网页上有一个链接发送用户到正确的博客post,但我似乎无法弄清楚如何在我的视图文件中使用我的路线。 每当我点击链接时,我都会得到这个错误:

{ "message": "Cast to ObjectId failed for value \":blogpost_id\" at path \"_id\"", "name": "CastError", "type": "ObjectId", "value": ":blogpost_id", "path": "_id" } 

这是如何设置我的GET方法的结果,或者我如何试图在我的视图内使用它?

url是localhost:8080/:blogpost_id ,这是不正确的。 “更多”链接是我正在谈论的。

routes.js

  var express = require('express'); var router = express.Router(); var blogDB = require('../config/blogDB.js'); var Blogpost = require('./models/blogModel.js'); var paginate = require('express-paginate'); //index router.use(paginate.middleware(10, 50)); router.route('/') // START POST method .post(function(req, res) { var blogpost = new Blogpost(); // create a new instance of a Blogpost model blogpost.title = req.body.title; // set the blog title blogpost.author = req.body.author; // set the author name blogpost.tagline = req.body.tagline; // set the tagline blogpost.content = req.body.content; // set the blog content blogpost.category = req.body.category; // set the category blogpost.tags = req.body.tags; // set the tags blogpost.date = req.body.date; // set the date of the post //Save Blog Post blogpost.save(function(err) { if (err) res.send(err); res.json({ message: 'Blog created.' }); }); }) // END POST method // START GET method .get(function(req, res, next) { Blogpost.paginate({}, req.query.page, req.query.limit, function(err, pageCount, blogpost, itemCount) { if (err) return next(err) if (err) res.send(err); blogpost.title = req.body.title; // get the blog title blogpost.author = req.body.author; // get the author name blogpost.tagline = req.body.tagline; // get tagline blogpost.content = req.body.content; // get the blog content blogpost.category = req.body.category; // get the category blogpost.tags = req.body.tags; // get the tags blogpost.date = req.body.date; // get the date of the post res.format({ html: function() { res.render('pages/index', { blogpost: blogpost, pageCount: pageCount, itemCount: itemCount }) }, json: function() { res.json({ object: 'blogpost', has_more: paginate.hasNextPages(req)(pageCount), data: blogpost }) } }); // END res.format(html, json) }); // END Blogpost.paginate }); // END GET method //Route for individual blogs router.route('/:blogpost_id') // START GET method blog by ID .get(function(req, res) { Blogpost.findById(req.params.blogpost_id, function(err, blog) { if (err) res.send(err); blogpost.title = req.body.title; // update the blog title blogpost.author = req.body.author; // update the author name blogpost.tagline = req.body.tagline; // update the tagline blogpost.content = req.body.content; // update the blog content blogpost.category = req.body.category; // update the category blogpost.tags = req.body.tags; //update the tags blogpost.date = req.body.date; // update the date of the post res.json(blog); res.render('/pages/blogpost'); }); }) // END GET method blog by ID // START PUT method .put(function(req, res) { Blogpost.findById(req.params.blogpost_id, function(err, blogpost) { if (err) res.send(err); blogpost.title = req.body.title; // update the blog title blogpost.author = req.body.author; // update the author name blogpost.tagline = req.body.tagline; // update the tagline blogpost.content = req.body.content; // update the blog content blogpost.category = req.body.category; // update the category blogpost.tags = req.body.tags; //update the tags blogpost.date = req.body.date; // update the date of the post blogpost.save(function(err) { if (err) res.send(err); res.json({ message: 'Blog updated.' }); }); }); }) // END PUT method // START DELETE method .delete(function(req, res) { Blogpost.remove({ _id: req.params.blogpost_id }, function(err, bear) { if (err) res.send(err); res.json({ message: 'Successfully deleted' }); }); }); //about router.get('/about', function(req, res) { res.render('pages/about'); }); module.exports = router; 

index.ejs

 <html> <head> <% include ../partials/head %> </head> <body> <header> <% include ../partials/header %> </header> <div class="grid"> <div class="col-1-1"> <div class="blog-content"> <% blogpost.forEach(function(blogpost) { %> <tr> <td><h2><a href="#" class="blog-title"><%= blogpost.title %></a></h2></td> <td><h3 class="blog-category"><%= blogpost.category %></h3> <td><h3 class="blog-tagline"><i><%= blogpost.tagline %></i></h3></td> <td><p><%= blogpost.content %></p></td> <td><a href="<%= /:blogpost_id %>" class="blog-read-more">Read More</a></td> </tr> <% }); %> </div> </div> </div> <div class="paginate"> <% include ../partials/paginate %> </div> <footer> <% include ../partials/footer %> </footer> </body> </html> 

我觉得这一行

 <a href="<%= /:blogpost_id %>" class="blog-read-more">Read More</a> 

应该

 <a href="/<%= blogpost.id %>" class="blog-read-more">Read More</a> 

以便在forEach循环中使用博客文章的ID。 您可能需要将博客post的ID添加到发送到视图的数据中,path不会显示在问题中。