求救啊,不然无法继续下去了,谁能帮我解决一下,实在没招了,感激不尽的说!var id=req.body.movie._id;
var express = require('express');
var path=require('path');
var _ = require('underscore');
var bodyParser = require('body-parser');
var port = process.env.PORT || 3000;
var app = express();
var mongoose=require( 'mongoose');
var Movie=require('./models/movie')
var bson = require('bson');
mongoose.connect('mongodb://localhost/node');
app.set('views','./views/pages')
app.engine('html', require('ejs').renderFile);
app.set('view engine', 'ejs');
app.use(bodyParser.urlencoded({ extended: true }))
app.use(express.static(path.join(__dirname,'bower_components')));
app.locals.moment = require('moment');
app.listen(port)
console.log('node started on port' + port)
//index page
app.get('/',function(req,res){
Movie.fetch(function(err,movies){
if(err){
console.log(err)
}
res.render('index.html',{
title:'imooc 首页',
movies:movies
})
})
})
//detail page
app.get('/movie/:id',function(req,res){
var id=req.params.id
Movie.findById(id,function(err,movie){
res.render('detail.html',{
title:'imooc'+movie.title,
movie:movie
})
})
})
//admin page
app.get('/admin/movie',function(req,res){
res.render('admin.html',{
title:'imooc 后台录入页',
movie:{
title:' ',
doctor:' ',
country:' ',
year:' ',
poster:' ',
flash:' ',
summary:' ',
language:' ',
}
})
})
//admin update movie
app.get('/admin/update/:id',function(req,res){
var id=req.params.id
if(id){
Movie.findById(id,function(err,movie){
res.render('admin.html',{
title:'imooc 后台更新页',
movie:movie
})
})
}
})
//admin post movie
app.post('/admin/movie/new',function(res,req){
var id=req.body.movie._id; //报错TypeError: Cannot read property 'movie' of undefined
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,
flash:movieObj.flash
})
_movie.save(function(err,movie){
if(err){
console.log(err)
}
res.redirect('/movie/'+movie._id)
})
}
})
//list page
app.get('/admin/list',function(req,res){
Movie.fetch(function(err,movies){
if(err){
console.log(err)
}
res.render('list.html',{
title:'imooc 列表页',
movies:movies
})
})
})