2014-08-30 69 views
0

我想使用PUT方法来更新我的数据库中的记录,但我遇到了未定义对象的问题。ExpressJS PUT方法undefined objecty问题

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.' }); 
      }); 

     }); 

    }); 

回答

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

应该是:

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

伟大号召,并感谢您的回答 – cphill 2014-08-30 22:57:56