gpt4 book ai didi

node.js - Expressjs路由问题

转载 作者:太空宇宙 更新时间:2023-11-03 23:39:48 26 4
gpt4 key购买 nike

我试图在我的页面上建立一个链接,将用户发送到正确的博客文章,但我似乎不知道如何在我的 View 文件中使用我的路线。每次我点击该链接时,都会出现以下错误:

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

这是我在路线上设置 GET 方法的结果还是我尝试在 View 中使用它的结果?

网址是 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;

索引.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。您可能需要将博客文章的 id 添加到发送到 View 的数据中,该路线未在问题中显示。

关于node.js - Expressjs路由问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25963918/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com