做到最后一步,一直显示Cannot read property '_id' of undefine ?
做到最后一步,一直显示Cannot read property '_id' of undefine ,把req.body输出发现是个空对象,为什么会这样呢?
做到最后一步,一直显示Cannot read property '_id' of undefine ,把req.body输出发现是个空对象,为什么会这样呢?
2016-12-16
你好 我后台录入消息点击录入按钮的时候,显示404 是怎么回事
index.js var express = require('express'); var router = express.Router(); var _ = require('underscore'); var Movie = require('../models/movie.js'); /* GET home page. */ //router.get('/', function(req, res, next) { // res.render('index', { title: 'My Videos' }); //}); //detail router.get('/movie/:id', function (req, res) { //通过req,params,id来拿到id参数 var id = req.params.id; //调用模式里面定义的静态方法 Movie.findById(id, function (err, movie) { res.render('detail', { title: 'imovie' + movie.title, movie: movie }) }) }); //admin-page router.get('/admin/movie', function (req, res) { res.render('admin', { title: 'imovie 后台录入页面', movie: { title: '', director: '', country: '', year: '', poster: '', flash: '', summary: '', language: '' } }) }); //admin updata movie//更新电影 router.get('/admin/update/:id', function (req, res) { //拿到这个id var id = req.params.id; //判断如果id存在 if (id) { Movie.findById(id, function (err, movie) { res.render('admin', { //传入这个电影 title: 'imovie 后台更新页', movie: movie }) }) } }); //admin post movie:拿到从后台录入页post过来的数据 router.post('/admin/movie/new', function (req, res) { //判断数据id是新是旧 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) } //用上面post过的新电影数据替换掉老的数据 //使用underscore模块 _movie = _.extend(movie, movieObj); _movie.save(function (err, movie) { if (err) { console.log(err) } //成功的话跳转到电影详情页 res.redirect('/movie/' + movie._id) }) }) } //如果没有id的话 else { //调用构造函数,传入电影数据 _movie = new Movie({ director: movieObj.director, title: movieObj.title, country: movieObj.country, language: movieObj.language, year: movieObj.year, poster: movieObj.poster, flash: movieObj.flash, summary: movieObj.summary }); _movie.save(function (err, movie) { if (err) { console.log(err) } //成功的话跳转到电影详情页 res.redirect('/movie/' + movie._id) }) } }); //list-page router.get('/admin/lists', function (req, res, next) { //查询一下 Movie.fetch(function (err, movies) { //如果异常,则打印出异常 if (err) { console.log(err) } //渲染lists模板 res.render('lists', { title: 'imovie 列表页', //把查询到的lists列表赋予,movies movies: movies }) }); }); //index page router.get('/', function (req, res) { //调用模型.Movie.fetch,直接调用movie数组 Movie.fetch(function (err, movies) { //如果异常,则打印出异常 if (err) { console.log(err) } //渲染首页模板 res.render('index', { title: 'imovie 首页', //把查询到的首页列表赋予,movies movies: movies }) }); //res.render('index', { // title: 'imovie 主页', // movies: [{}] //}) }); module.exports = router; app.js var express = require('express'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var bodyParser = require('body-parser'); var index = require('./routes/index'); var mongoose = require('mongoose'); var users = require('./routes/users'); //underscore模块 var _ = require('underscore'); var app = express(); mongoose.connect('mongodb://localhost/imovie'); //http.createServer(app).listen(app.get('port'), function(){ // console.log('Express server listening on port ' + app.get('port')); //}); app.locals.moment=require('moment'); // view engine setup //更改视图的默认目录 app.set('views', path.join(__dirname, './views/pages')); app.set('view engine', 'jade'); //后台录入也有提交表单的步骤,所以可以格式化后台表单数据 app.use(bodyParser.urlencoded({extended: true})); //设置静态资源(css,js等)从public文件夹中获取 app.use(express.static(path.join(__dirname, 'public'))); // uncomment after placing your favicon in /public //app.use(favicon(path.join(__dirname, 'public', 'favicon.ico'))); app.use(logger('dev')); app.use(bodyParser.json()); app.use(cookieParser()); app.use('/', index); app.use('/users', users); //app.use('/movie/:id', index); //app.use('/admin/movie', index); //app.use('/admin/lists', index); //app.use('/admin/movie',users); // catch 404 and forward to error handler app.use(function (req, res, next) { var err = new Error('Not Found'); err.status = 404; next(err); }); // error handler app.use(function (err, req, res, next) { // set locals, only providing error in development res.locals.message = err.message; res.locals.error = req.app.get('env') === 'development' ? err : {}; // render the error page res.status(err.status || 500); res.render('error'); }); module.exports = app; schemas/movie.js //引入mongoose模块 var mongoose = require('mongoose'); //调用mongoose的schema方法 var MovieSchema = new mongoose.Schema({ //对数据定义类型 director: String, title: String, language: String, country: String, flash: String, poster: String, year: Number, //更新数据时时间的记录 meta: { createAt: { type: Date, //默认值:创建时候的时间 default: Date.now() }, //更新的时间 updateAt: { type: Date, default: Date.now() } } }); //为这个模式添加一个方法; //每次存储数据之前都来调用这个方法:pre.save MovieSchema.pre('save', function (next) { //判断数据是否是新加的,如果是的话,那么词条数据的更新时间就等于创建时间 if (this.isNew) { this.meta.createAt = this.meta.updateAt = Date.now() } else { //否则的话,跟新时间就等于现在时间 this.meta.updateAt = Date.now() } //用next结尾 next() }); //增加一个静态方法 //静态方法不会直接与数据库交互,只有经过model编译并且实例化之后才会有这种方法 MovieSchema.statics = { //添加fetch方法,用来取出数据库所有数据, fetch: function (cb) { return this .find({}) //按照跟新时间排序 .sort('meta.updateAt') //执行这个回掉方法 .exec(cb) }, //用来查询单条数据 findById: function (id, cb) { return this .findOne({"_id": id}) .exec(cb) } }; //将此模式导出 module.exports = MovieSchema; models/movie.js var mongoose = require('mongoose'); //引入Schema模式文件 var MovieSchema = require('../schemas/movie.js'); //编译生成Movie模型,通过调用mongoose.model,传入这个模型的名字(Movie)以及模式(MovieSchema)。 var Movie = mongoose.model('Movie', MovieSchema); //导出构造函数movie module.exports = Movie;
举报