2015-06-14 102 views
1

我有一個關於貓鼬架構創建問題,當我運行貓鼬+ Node.js的 當我運行應用程序,我收到以下錯誤:貓鼬模式創建錯誤

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

它應該是var MovieSchema = new mongoose.Schema(..),而不是mongoose.Scheme(..)

+0

我使用var MovieSchema = new mongoose.Schema(..),你可以檢查我的movie.js第二行 – XBroder

+0

不,你用mongoose.Scheme(用e)。你需要使用mongoose.Schema(與一個)。再次檢查。 –

+0

我想我找到了我的錯誤,非常感謝,這是我第一次使用stackoverflow – XBroder