一旦编译模型''编译一次(或者我已经监督了某些东西),就不能覆盖`User`模型

时间:2016-09-15 06:36:31

标签: javascript node.js mongodb mongoose

你可以帮帮我吗?我不明白这里有什么不对。

以下是来自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") ]

谢谢!

1 个答案:

答案 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,
}));