2

could you help me? I can't see what's wrong here.

Here are the code snippets from 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,
   });

This appears every time "noding" server.js. Btw. I'm a newbie in node.js/mongoose.

The error from the terminal:

/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)

Maybe helpful - 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") ]

Thanks!

mandaleybro
  • 23
  • 2
  • 7
  • Update: I tried to find the solution here: http://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose?noredirect=1&lq=1 but I didn't got the problem solved with the answers, which is why I opened this thread here. My problem is only based on a single file called server.js, while the other similar problems I found were based on more files i.e. insert.js and app.js in a relation. – mandaleybro Sep 15 '16 at 12:28

1 Answers1

1

You have made a mistake while initializing your mongoose model and have not followed the proper syntax and docs. Refer the mongoose doc at this link.

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,
}));
Ananth Pai
  • 1,939
  • 14
  • 15
  • Okay, I changed it and tried it with the next model as well but I still receive the same error. Code looks like this now: const Schema = mongoose.Schema; // The User var User = mongoose.model('User', new Schema({ username: String, password: String, biography: String, image: String, })); var Status = mongoose.model('Status', new Schema({ body: String, time: Number, username: String, image: String, comments: Array, likes: Array })); – mandaleybro Sep 15 '16 at 07:33
  • MongoDb by default runs on port `27017`, specify the port number as well when you are trying to make the connection. – Ananth Pai Sep 15 '16 at 07:51
  • I changed this line for the port on which the server will listen to `var port = 27017;` but nothing changes. Same issue. – mandaleybro Sep 15 '16 at 08:18