2016-09-15 80 views
2

你能幫助我嗎?我看不出這裏有什麼問題。一旦編譯了模型'',編譯一次就不能覆蓋`User`模型

這裏是代碼片段從server.js:

var mongoose = require('mongoose'); 

var MongoClient = require('mongodb').MongoClient 
    , assert = require('assert'); 
var db = 'nodebook'; 
var url = '' 
mongoose.createConnection('mongodb://localhost/'+db); 

// The User 
    var User = mongoose.model('User', { 
    username: String, 
    password: String, 
    biography: String, 
    image: String, 
    }); 

這似乎每次 「noding」 server.js。 Btw。我是node.js/mongoose中的新手。

來自終端的錯誤:

/Users/davidnoldner/node_modules/mongoose/lib/index.js:360 
     throw new mongoose.Error.OverwriteModelError(name); 
    ^
OverwriteModelError: Cannot overwrite `User` model once compiled. 
    at Mongoose.model (/Users/davidnoldner/node_modules/mongoose/lib/index.js:360:13) 
    at Server.<anonymous> (/Users/davidnoldner/Librament/server.js:46:21) 
    at emitTwo (events.js:87:13) 
    at Server.emit (events.js:172:7) 
    at HTTPParser.parserOnIncoming [as onIncoming] (_http_server.js:533:12) 
    at HTTPParser.parserOnHeadersComplete (_http_common.js:103:23) 

也許有幫助 - mongod的控制檯:

2016-09-15T08:32:07.973+0200 W NETWORK [HostnameCanonicalizationWorker] Failed to obtain name info for: [ (2620:9b::5dd:4570, "nodename nor servname provided, or not known"), (2620:9b::5dd:4570, "nodename nor servname provided, or not known"), (192.168.0.16, "nodename nor servname provided, or not known"), (5.221.69.112, "nodename nor servname provided, or not known"), (192.168.0.16, "nodename nor servname provided, or not known"), (5.221.69.112, "nodename nor servname provided, or not known") ] 

謝謝!

+0

更新: 我試圖在這裏找到解決方案:http://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose?noredirect=1&lq=1但我沒有」問題解決了答案,這就是爲什麼我在這裏打開這個線程。 我的問題只基於一個名爲server.js的文件,而我發現的其他類似問題則基於更多文件,即關係中的insert.js和app.js。 –

回答

1

初始化貓鼬模型時出錯,沒有遵循正確的語法和文檔。請參閱this鏈接上的mongoose文檔。

var mongoose = require('mongoose'); 

var MongoClient = require('mongodb').MongoClient 
    ,assert = require('assert'); 
var db = 'nodebook'; 
var url = '' 
mongoose.createConnection('mongodb://localhost:27017/'+db); 

const Schema = mongoose.Schema; 
// The User 
var User = mongoose.model('User', new Schema({ 
    username: String, 
    password: String, 
    biography: String, 
    image: String, 
})); 
+0

好吧,我改變了它,並嘗試與下一個模型,但我仍然收到相同的錯誤。代碼如下: const Schema = mongoose.Schema; //用戶 VAR用戶= mongoose.model( '用戶',新的架構({ 用戶名:字符串, 密碼:字符串, 傳記:字符串, 圖像:字符串, })); VAR狀態= mongoose.model( '狀態',新的架構({ \t體:字符串, \t時間:號碼, \t用戶名:字符串, \t圖像:字符串, \t評論:數組, \t喜歡:Array })); –

+0

默認情況下,MongoDb在端口'27017'上運行,指定端口號以及在嘗試建立連接時。 –

+0

我改變了這條線,服務器將偵聽'var port = 27017;'的端口,但沒有任何變化。同樣的問題。 –