猿问

在node + mongodb建站时出现的问题

我用的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;


求大神们帮忙!!!

LNoe
浏览 2245回答 5
5回答

程序员熊明才

之后会遇到这个报错    !  求解决分享Warning: missing space before text for line 35 of jade file "/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/views/pages/admin.jade"Warning: missing space before text for line 35 of jade file "/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/views/pages/admin.jade"take helloundefined{ MongooseError: Movie validation failed/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/app.js:173    at ValidationError (/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/node_modules/mongoose/lib/error/validation.js:22:11)        res.redirect('/movie/'+_movie._id);    at model.Document.invalidate (/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/node_modules/mongoose/lib/document.js:1410:32)                                     ^    at model.Document.set (/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/node_modules/mongoose/lib/document.js:695:10)TypeError: Cannot read property '_id' of undefined    at /Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/app.js:173:38    at model.Document.set (/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/node_modules/mongoose/lib/document.js:540:18)    at model.Document (/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/node_modules/mongoose/lib/document.js:66:10)    at model.Model (/Users/xiongmingcai/GitHub/viewapp/Nodeweb-master/node_modules/mongoose/lib/model.js:41:12)

liqian888

你找到解决办法没?我也遇到这个问题

_潇潇暮雨

检查一下第一个文件的13行_movie是否取到了正确的值

郑家好人

请教一下isNew是什么意思?
随时随地看视频慕课网APP

相关分类

Node.js
我要回答