Mongoose架构创建错误

时间:2015-06-14 12:57:25

标签: node.js mongodb

当我运行mongoose + node.js时,我遇到了有关Mongoose模式创建的问题 当我运行应用程序时,我收到以下错误:

Users/tyrant/workspace/myworkspace/nodeProjects/imooc/schemas/movie.js:3
var MovieSchema = new mongoose.Scheme({
              ^
TypeError: undefined is not a function
      at Object.<anonymous>     (/Users/tyrant/workspace/myworkspace/nodeProjects/imooc/schemas/movie.js:3:19)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/tyrant/workspace/myworkspace/nodeProjects/imooc/models/movie.js:2:19)
at Module._compile (module.js:460:26)
at Object.Module._extensions..js (module.js:478:10)
at Module.load (module.js:355:32)
at Function.Module._load (module.js:310:12)
at Module.require (module.js:365:17)
at require (module.js:384:17)
at Object.<anonymous> (/Users/tyrant/workspace/myworkspace/nodeProjects/imooc/app.js:5:13)
at Module._compile (module.js:460:26)

继承我的movie.js代码

var mongoose = require('mongoose')
var MovieSchema = new mongoose.Scheme({
    doctor: String,
    title: String,
    language: String,
    country: String,
    summary: String,
    flash: String,
    poster: String,
    year: String,
    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(cb){
        return this
            .find({})
            .sort('meta.updateAt')
            .exec(cb)
    },

findById:function(id, cb){
    return this
        .findOne({_id: id})
        .exec(cb)
    }
}

module.exports = MovieSchema

这是node.js代码的一部分

var express = require('express');   //加载express模块
var port = process.env.PORT || 3000;    //设置端口
var mongoose = require('mongoose')
var _ = require('underscore')
var Movie = require('./models/movie')
var bodyParser = require("body-parser")
var path = require('path')
var app = express();    //启动一个web服务器
mongoose.connect('mongodb://localhost/imooc')

我无法弄清楚错误的原因。

1 个答案:

答案 0 :(得分:1)

它应该是var MovieSchema = new mongoose.Schema(..),而不是mongoose.Scheme(..)