以下是来自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时都会出现这种情况。 顺便说一句。我是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 console:
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 :(得分:1)
您在初始化mongoose模型时犯了一个错误,并且没有遵循正确的语法和文档。请参阅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,
}));