我學習的平均堆,當我嘗試使用來啓動服務器貓鼬架構尚未註冊模式
npm start
我得到一個異常說:
schema hasn't been registered for model 'Post'. Use mongoose.model(name, schema)
這裏是我的代碼裏面/models/Posts.js
var mongoose = require('mongoose');
var PostSchema = new mongoose.Schema({
title: String,
link: String,
upvotes: { type: Number, default: 0 },
comments: [{ type: mongoose.Schema.Types.ObjectId, ref: 'Comment' }]
});
mongoose.model('Post', PostSchema);
,我可以看到的架構應註冊爲模型「郵政」,但什麼C可能導致異常被拋出?
在此先感謝。
編輯:這裏的異常錯誤
/home/arash/Documents/projects/personal/flapper-news/node_modules/mongoose/lib/index.js:323
throw new mongoose.Error.MissingSchemaError(name);
^
MissingSchemaError: Schema hasn't been registered for model "Post".
Use mongoose.model(name, schema)
和這裏的app.js代碼與貓鼬初始化:
var mongoose = require('mongoose');
mongoose.connect('mongodb://localhost/news');
require('./models/Posts');
require('./models/Comments');
前行:
app.use('/', routes);
你犯了一個錯誤在其他地方。上面的代碼是有效的。也許你在某處「需要」(Post)「Post.js」,但是你從未「導出」模型。 – 2014-11-08 14:29:13
@NeilLunn好的,我將編輯與我的異常錯誤的問題,因爲這是我可以從它讀取的。也許其他人可以看到我看不到 – 2014-11-08 14:30:40
@Umm。你以後「需要」你有沒有「出口」?我認爲這是在這裏失蹤的代碼。 – 2014-11-08 14:33:38