【问题标题】:Expressjs Routing IssueExpressjs 路由问题
【发布时间】:2014-11-15 19:39:50
【问题描述】:

我试图让我的页面上的链接将用户发送到正确的博客文章,但我似乎无法弄清楚如何在我的视图文件中使用我的路线。每次点击链接都会报这个错误:

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

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

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

【问题讨论】:

    标签: node.js mongodb express ejs


    【解决方案1】:

    我觉得这条线

    <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 添加到发送到视图的数据中,问题中未显示路线。

    【讨论】:

    • 嘿@Russ Cam,抱歉我没有注意到我遗漏了部分路线文件。有关已编辑的路线文件,请参见上文。我最初使用forEach 循环进行博客路由,但它阻止了我的分页工作。我假设通过省略循环是这不起作用的原因。你同意吗?
    • 您有两个与博客文章相关的 GET 路由,"/" 返回博客文章的集合(页面),"/:blogpost_id" 返回 id 与 :blogpost_id 的值匹配的博客文章路由参数。 forEach 用于遍历视图中的博客文章,因此对于每个博客文章,阅读更多链接应该使用博客文章的 id。看起来您的 "/" 正在尝试处理一篇博客文章,而它应该按照此处的 express js 分页示例处理一组博客文章 github.com/expressjs/express-paginate
    猜你喜欢
    • 1970-01-01
    • 2013-11-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多