2014-09-21 157 views
1

我正在嘗試將頁面上的鏈接發送給正確的blogpost,但我似乎無法弄清楚如何在我的視圖文件中使用我的路由。我每次點擊的鏈接,我給出的這個錯誤:Expressjs路由問題

{ 
    "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這是不正確的。 「Read More」鏈接就是我正在談論的內容。

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> 

回答

2

我覺得這條線

<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添加到發送到視圖的數據中,路徑不會顯示在問題中。

+0

嘿@Russ Cam,對不起,我沒有注意到我遺漏了部分路徑文件。請參閱上面的編輯路線文件。我最初爲博客路由使用了'forEach'循環,但它阻止了我的分頁工作。我假設通過忽略循環是這個不起作用的原因。你會同意嗎? – cphill 2014-09-21 23:27:38

+0

您有兩條與博客文章相關的GET路由,''/「''返回博客文章的集合(頁面)和'」/:blogpost_id「',它返回的博客文章的id與':blogpost_id'的值匹配路線參數。 'forEach'用於迭代視圖中的博客文章,因此對於每篇博文,「閱讀更多」鏈接應使用博客文章的ID。看起來你的'「/」'試圖與一篇博客文章一起工作,當它應該與一組博客帖子一起工作時,按照這裏的快速js分頁示例https://github.com/expressjs/express-paginate – 2014-09-21 23:55:03