node.js数据库操作
数据库save方法 提示这个错误怎么解决TypeError: Cannot read property 'save' of null
数据库save方法 提示这个错误怎么解决TypeError: Cannot read property 'save' of null
2017-05-10
schemas/movie.js
MovieSchema.pre('save',function(next){
if(this.isNew){
this.meta.createAt=this.meta.updateAt=Date.now();
}else{
this.meta.updateAt=Date.now();
}
next();
});
models/movie.js
var mongoose=require('mongoose');
var MovieSchema=require('../schemas/movie');
var Movie=mongoose.model('Movie',MovieSchema);
module.exports=Movie;
app.js
//引入models/movie.js
var Movie=require('./models/movie');
//admin post movie
app.post('/admin/movie/new',function(req,res){
var id=req.body.movie._id;
var movieObj=req.body.movie;
var _movie=null;
if(id!=='undefined'){
Movie.findById(id,function(err,movie){
if(err){
console.log('Error: '+err);
}
_movie=underscore.extend(movie,movieObj);
_movie.save(function(err,movie){
if(err){
console.log('Error: '+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('Error: '+err);
}
res.redirect('/movie/'+_movie._id);
});
}
});
看看哪里写错了。
举报