2013-08-24 59 views
21

我有Node.js的一個典型項目 - 快速3 - MongoDB的MissingSchemaError:架構尚未註冊爲模型

我想在我的/途徑進行查詢到我的模式「分享Tweet」/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... 

這是我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的一部分

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

你不會顯示你要調用'require'來把'index.js'放到'app.js'中,但是如果在你使用'mongoose.model'調用註冊模式之前,那就是你的問題。 – JohnnyHK

+0

文件/路由/ index.js自動調用表示,如果我沒有錯的是在這條線 \t'app.get(「/」,routes.index);' 也就是說以後註冊模式作爲你可以在app.js的代碼中看到 –

+0

它不會自動調用,它是在生成的app.js代碼中的這一行:'var routes = require('./ routes');' – JohnnyHK

回答

46

index.js文件在您app.js文件調用:

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

所以一定是被呼叫您的通話註冊'Teewt'模式作爲app.js貓鼬模型。

+0

這對我來說非常適合。謝謝:) – Timbergus

+0

#feelLikeANoob! –

1

以不同方式命名您的模式和模型。重新聲明Teewt是一種JavaScript「不好的部分」,也是任何編程語言中的錯誤。只需調用架構TeewtSchema和模型Teewt(因爲架構通常僅用於應用程序中的一個文件中,但模型可能會廣泛使用)。執行

+0

你是對的這是不好的做法,改變名字的變化,但不解決錯誤 index.js顯然運行第一個註冊模式 –

+0

是的,的確,JohnnyHK的評論可能是你的根本原因。 –

+1

你理解他的評論也不是很清楚。在'app.js'中找到'var routes = require('routes');'的行,並在文件中將它移到較低的位置,以便在配置完貓鼬模型之後。 –

0
換句話說

,這條線(從app.js):

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

應鳴叫登記之後被調用,在該行(在index.js):

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

我也有這個錯誤。很長時間以來,困擾着這個和類似的線程尋求幫助,試圖調試。最終,問題原來是由於我的情況下貓鼬版本。我正在嘗試使用貓鼬燈具來播種一些默認數據以開始使用mongo。

試試這個:刪除node_modules目錄中的項目,運行npm cache clean,然後運行npm install。如果即使這不起作用,請嘗試比較mongoose/mongoose-fixture版本與有問題的應用程序之間的差異,然後嘗試更改package.json中的版本,然後重複上述步驟。這對我有效。