var express = require('express') var path = require('path') var mongoose = require('mongoose') var port = process.env.PORT || 3000 var _ = require ('underscore') var Movie = require('./models/movie') var app = express() var serveStatic = require('serve-static') var bodyParser = require('body-parser') mongoose.connect('mongodb://localhost/imooc') app.set('views', './views/pages') app.set('view engine', 'pug') app.use(bodyParser.urlencoded({extended:true})) app.use(bodyParser.json()) app.use(express.static(path.join(__dirname, 'bower_components'))) app.listen(port) app.use(serveStatic('bower_components')) app.use(require('body-parser').urlencoded({extended:true})) console.log('immoc started on port ' + port) // index page app.get('/', function(req, res) { Movie.fetch(function(err, movies) { if (err) { console.log(err) } res.render('index', { title: 'imooc 首页', movies: movies }) }) }) //中间忽略 // admin post movie app.post('/admin/movie/new', function(res, req) { var id = req.body.movie._id var movieObj = req.body.movie var _movie if (id !== 'undefined') { Movie.findById(id, function(err, movie) { if (err) { console.log(err) } _movie = _.extend(movie, movieObj) _movie.save(function(err, movie) { if (err) { console.log(err) } res.redirect('/movie/' + movie._id) }) }) } else { _movie = new Movie({ doctor: movieObj.doctor, title: movieObj.title, country: movieObj.country, language: movieObj.language, year: movieObj.year, poster: movieObj.poster, summary: movieObj.summary, })
如题,按录入时报错TypeError: Cannot read property 'movie' of undefined。
我已经安装了body-parser,还是报错。
是因为最新版的引用方式改了吗?
相关分类