我用的express为4.13.1,mongoose为4.4.4,而且我跟老师讲的目录结构不太一样,用的webstorm新建的nodejs项目
在录入页post后出现如下错误:
_movie.save(function(err, movie){ ^ TypeError: Cannot read property 'save' of null
这是我出现错误的代码位置:
//admin post movie router.post('/admin/movie/new', function(req, res){ 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, poster: movieObj.poster, year: movieObj.year, summary: movieObj.summary, flash: movieObj.flash }); _movie.save(function(err, movie){ if (err){ console.log(err); } res.redirect('/movie/' + movie._id); }); } });
var mongoose = require('mongoose'); var MovieSchema = new mongoose.Schema({ title: String, doctor: String, language: String, country: String, summary: String, flash: String, poster: String, year: Number, meta: { createAt: { type: Date, default: Date.now() }, updateAt: { type: Date, default: Date.now() } } }); MovieSchema.pre('save', function(next){ if (this.isNew){ this.meta.createAt = this.meta.updateAt = Date.now(); }else { this.meta.updateAt = Date.now(); } next(); }); MovieSchema.statics = { fetch: function(callback){ return this .find({}) .sort('meta.updateAt') .exec(callback) }, findById: function(id, callback){ return this .findOne({_id: id}) .exec(callback) } }; module.exports = MovieSchema;
var mongoose = require('mongoose'); var MovieSchema = require('../schemas/movie'); var Movie = mongoose.model('Movie', MovieSchema); module.exports = Movie;
求大神们帮忙!!!
程序员熊明才
liqian888
_潇潇暮雨
郑家好人
相关分类