ExpressJS PUT方法未定义的对象问题

时间:2014-08-30 22:30:33

标签: javascript node.js express routes

我正在尝试使用PUT方法更新数据库中的记录,但我遇到的问题是没有定义对象。

ReferenceError: blogpost is not defined

我在路由步骤中引用了这个tutorial,并注意到尽管我的/blogs路由中定义了变量,这意味着它是该函数的本地变量,在教程中,它们不是'在路由put方法时再次定义变量。他们只是调用他们计划更新的对象属性。我有无法访问此对象的原因吗?这是范围问题吗?

routes.js:

  var express = require('express');
    var router = express.Router();
    var blogDB = require('../config/blogDB.js');
    var Blogpost = require('./models/blogModel.js');

    //index
        router.route('/')
        .get(function(req, res) {
        var drinks = [
                {   name: 'Bloody Mary', drunkness: 3 },
                {   name: 'Martini', drunkness: 5 },
                {   name: 'Scotch', drunkness: 10}
            ];

            var tagline = "Lets do this.";

            res.render('pages/index', {
                drinks: drinks,
                tagline: tagline
            });
        });






    //blog
        router.route('/blog') 

            // 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.content = req.body.content; // set the blog content

                    //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) {
                Blogpost.find(function(err, blogs) {
                    if (err)
                        res.send(err);

                    res.json(blogs);
                });
            }); // END GET method


        //Route for individual blogs
        router.route('/blog/: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);
                res.json(blog);
            });
        }) // END GET method blog by ID

        // START PUT method
        .put(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.content = req.body.content; // update the blog content

                blogpost.save(function(err) {
                    if (err)
                        res.send(err);


                    res.json({ message: 'Blog updated.' });
                });

            });

        });


    //about
        router.get('/about', function(req, res) {
                res.render('pages/about');
        });


    module.exports = router;

创建问题的特定区域:

// START PUT method
    .put(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.content = req.body.content; // update the blog content

            blogpost.save(function(err) {
                if (err)
                    res.send(err);


                res.json({ message: 'Blog updated.' });
            });

        });

    });

1 个答案:

答案 0 :(得分:0)

Blogpost.findById(req.params.blogpost_id, function(err, blog) {

应该是:

Blogpost.findById(req.params.blogpost_id, function(err, blogpost) {