2013-08-24 9 views
21

のために登録されていない - エクスプレス3 - MongoDBの私は/ルートに私のモデルのツイート」にクエリを作成しようとしているMissingSchemaError:スキーマは、私はNode.jsの持つ典型的なプロジェクトを持っているモデル

/index.jsと私は私のアプリは、これは私のapp.js

var mongoose = require('mongoose'); 

mongoose.connect('mongodb://localhost/fanOcesa'); 
var Schema = mongoose.Schema; 
var ObjectID = Schema.ObjectID; 

var Teewt = new Schema({ 
    cuerpo: String 
}); 

var Teewt = mongoose.model('Teewt', Teewt); 

var app = express(); 

app.set('port', process.env.PORT || 3000); 
app.set('views', __dirname + '/views'); 
app.set('view engine', 'jade'); 
app.use(express.favicon()); 
app.use(express.logger('dev')); 
app.use(express.bodyParser()); 
app.use(express.methodOverride()); 
app.use(app.router); 
app.use(express.static(path.join(__dirname, 'public'))); 

// development only 
if ('development' == app.get('env')) { 
    app.use(express.errorHandler()); 
} 

app.get('/', routes.index); 
app.get('/users', user.list); 

の一部であり、これは私のindex.jsの一部である

24 Aug 11:35:07 - [nodemon] starting `node app.js` 

/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286 
    throw new mongoose.Error.MissingSchemaError(name); 
     ^
MissingSchemaError: Schema hasn't been registered for model "Teewt". 
Use mongoose.model(name, schema) 
at Mongoose.model (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/node_modules/mongoose/lib/index.js:286:13) 
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/routes/index.js:6:33) 
at Module._compile (module.js:456:26) 
at Object.Module._extensions..js (module.js:474:10) 
at Module.load (module.js:356:32) 
at Function.Module._load (module.js:312:12) 
at Module.require (module.js:364:17) 
at require (module.js:380:17) 
at Object.<anonymous> (/Applications/XAMPP/xamppfiles/htdocs/ocesa/fanocesa/app.js:7:14) 
at Module._compile (module.js:456:26) 
24 Aug 11:35:07 - [nodemon] app crashed - waiting for file changes before starting... 

クラッシュ実行すると

var Teewt = require('mongoose').model('Teewt'); 

Teewt.find({}, function(err, docs){ 
    console.log('docs'); 
    console.log(docs); 
}); 

exports.index = function(req, res){ 
    res.render('index', { 
     docs: docs 
    }); 
}; 

このクエリを実行する正しい方法はありますか?

+2

「index.js」を 'app.js'に持っていくために' require'を呼び出す場所は表示されませんが、それが 'mongoose.model'呼び出しでスキーマを登録する前なら、それはあなたのものです問題。 – JohnnyHK

+0

ファイル/ routes/index.jsは、この行に間違っていないと自動的に呼び出す。 \t 'app.get( '/'、routes.index);' これは、あなたはapp.jsのコードで見ることができます –

+0

自動的に呼び出されるのではなく、生成されたapp.jsコードの次の行です: 'var routes = require( './ routes');' – JohnnyHK

答えて

46

index.jsファイルが実行されます。換言すれば

+0

これは私にとって完璧です。ありがとう:) – Timbergus

+0

#feelLikeANoob! –

1

スキーマとモデルに異なる名前を付けます。再宣言するTeewtは、JavaScriptの「悪い部分」であり、プログラミング言語の間違いでもあります。スキーマTeewtSchemaとモデルTeewtを呼び出すだけです(通常、スキーマはアプリケーションの1つのファイルでのみ使用されますが、モデルは広範囲に使用される可能性があるため)。

var routes = require('./routes'); 

だからapp.jsでマングースモデルとして'Teewt'スキーマを登録するには後にあなたの電話を呼ばれています確認してください。あなたのapp.jsファイルの呼び出しがどこ

+0

あなたは間違っていると思いますが、変数の名前を変更してもエラーは解決しません。 index.jsが最初にスキーマを登録して実行されています –

+0

はい、実際はJohnnyHKのコメントが原因です。 –

+1

彼のコメントを理解しているかどうかは不明です。 'var routes = require( 'routes');'を実行する 'app.js'の行を見つけ、それをファイルの下に移動して、あなたのmongooseモデルが設定された後に来るようにします。 –

0

、(app.jsから)この行:さえずるは(index.jsで)その行に、登録された後

var Teewt = mongoose.model('Teewt', Teewt); 

が呼び出されるべきである:

var Teewt = require('mongoose').model('Teewt'); 
1

私もこのエラーがあります。長い間困惑していましたが、このスレッドや類似のスレッドに続いて、デバッグしようとしました。結局のところ、問題は私のケースではマングースバージョンに起因することが判明しました。私はmongoose-fixtureを使用して、mongoで始まるいくつかのデフォルトデータを作成しようとしていました。

これを試してください:プロジェクトのnode_modulesディレクトリを削除し、npm cache cleannpm installを実行してください。これでも問題が解決しない場合は、mongoose/mongoose-fixtureのバージョンと問題のあるアプリと問題のアプリを比較して、package.jsonのバージョンを変更して上記の手順を繰り返してみてください。これは私のために働いた。

関連する問題