0

I am working with an Express.js application using Mongoose, and I'm a little unclear on what I was starting to morph on Cannot instantiate mongoose schema: "Object is not a function" where a separate question would probably be appropriate.

I have a schema that exists in MongoDB; from the MongoDB shell:

Inner Sanctum ~ $ mongo
MongoDB shell version: 2.6.7
connecting to: test
Welcome to the MongoDB shell.
For interactive help, type "help".
For more comprehensive documentation, see
    http://docs.mongodb.org/
Questions? Try the support group
    http://groups.google.com/group/mongodb-user
> use Pragmatometer;
switched to db Pragmatometer
> db.userInfo.insert({'username': '*****', 'password': '*****'});
WriteResult({ "nInserted" : 1 })
> 

While I will eventually need to have my Express application add a userInfo schema where there was none previously, my present code appears to be telling mongoose to create the schema from scratch, and it is getting an exception saying "This is already defined; you can't redefine it." The code I have is:

var schema = mongoose.Schema;
var user_details = new schema(
  {
  username: String,
  password: String
  },
  {
  collection: 'userInfo'
  });
var user = mongoose.model('userInfo', user_details);

What is an appropriate way to say, with mongoose, "I want to create/read/update/destroy a username-password record in the userInfo table"?

--UPDATE--

The exact error trace is:

/Users/jonathan/node_modules/mongoose/lib/index.js:340
      throw new mongoose.Error.OverwriteModelError(name);
            ^
OverwriteModelError: Cannot overwrite `userInfo` model once compiled.
    at Mongoose.model (/Users/jonathan/node_modules/mongoose/lib/index.js:340:13)
    at Object.<anonymous> (/Users/jonathan/server/app.js:51:21)
    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> (/Users/jonathan/server/bin/www:7:11)
    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 Function.Module.runMain (module.js:497:10)
    at startup (node.js:119:16)
    at node.js:929:3

The reference to (/Users/jonathan/server/app.js:51:21) appears to be the last line of the code above, var user = mongoose.model('userInfo', user_details);. Thanks,

Community
  • 1
  • 1
Christos Hayward
  • 5,777
  • 17
  • 58
  • 113
  • Where exactly do you get the message `This is already defined; you can't redefine it.`? What line of code causes that error? – x-ray Jul 03 '15 at 19:41
  • The words you quoted were my paraphrase; I've quoted the error and line of code in an expansion to the question. Thanks... – Christos Hayward Jul 03 '15 at 20:40
  • Maybe this helps? http://stackoverflow.com/questions/19051041/cannot-overwrite-model-once-compiled-mongoose – x-ray Jul 03 '15 at 22:49

0 Answers0