2014-08-30 30 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