node +mongodb建站问题 Movie.fetch is not a function

//app.js
var express = require('express')
var path = require('path')
var mongoose = require('mongoose')
var _ = require('underscore')
var bodyParser  = require('body-parser');
var Movie = require('./models/movie')
var port = process.env.PORT || 3000
var app = express();

mongoose.connect('mongodb://localhost/imooc')

app.set('views',__dirname+'/views/pages')   
app.set('view engine','jade')               
app.use(bodyParser.urlencoded({ extended: false }))
app.use(express.static(path.join(__dirname, 'res')));
app.listen(port)


console.log('imooc started on port '+ port)

//index page
app.get('/',(req,res)=>{
    Movie.fetch(function(err,movies){
        if(err){
            console.log(err)
        }
        res.render('index',{
            title:'imooc 首页',
            movies:movies
        })
    })
})

// schemas\movie.js
var mongoose = require('mongoose')
var MovieSchema = require('../schemas/movie')
var Movie = mongoose.model('Movie',MovieSchema)

module.exports = Movie

//models\movies.js
var mongoose = require('mongoose')

var MovieSchema = new mongoose.Schema({
    doctor: String,
    title: String,
    language: String,
    country: String,
    summary: String,
    flash: String,
    poster: String,
    year: Number,
    meta:{
        createAt:{
            type:Date,
            default:Date.now()
        },
        updataAt:{
            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.static = {
    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

TypeError: Movie.fetch is not a function

Aditya
浏览 2390回答 1
1回答

蜂之谷

MovieSchema.statics   
打开App,查看更多内容
随时随地看视频慕课网APP