Expressjs数据未传递给视图

时间:2014-09-25 00:31:21

标签: node.js express get

我设置了一条路由,可以根据其ID访问各个数据库记录。在我的主页('/')上,我正在提取所有记录,但每页只显示10条记录,其中包含express-paginate模块。当我点击Read More时,我被发送到相应的记录页面,但出于某种原因,我没有看到任何CSS样式,而且我传递给此视图的数据将以{{{ 1}}。 / blog / blogpost_id

正在访问单个记录

routes.js:

undefined

blogpost.ejs

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('/blog/:blogpost_id')


    // START GET method blog by ID  
    .get(function(req, res) {

        var blogpost = new Blogpost(); // create a new instance of a Blogpost model


        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


            //res.json(blogpost);
            res.render('pages/blogpost', {
                blogpost: 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;

1 个答案:

答案 0 :(得分:0)

您正在使用router.route('/blog/:blogpost_id').get()方法覆盖博客帖子内容。见这些:

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

...你覆盖了你刚刚获得的博客帖子(最有可能)空变量,因为这是你的GET方法。