Reputation: 5933
I am trying to create a mongoose model and using it to get the data from the mongodb, but I am getting the exception
My mongoose version from package.json is :"mongoose": "4.5.5"
TypeError: Cannot read property 'users' of undefined
Now here is my demo code:
var mongoose = require("mongoose");
var schema = mongoose.Schema;
var Users = {
_id : {
type : String,
require : true
},
username : {
type : String,
required : true
},
password : {
type : String,
required : true
}
};
var UsersModel = new mongoose.model('users', Users);
var newUser = new UsersModel({"username":"A", "password":"V"});
newUser.save(function(err, result){
if(err){
console.log('>>>>>> Error');
}else{
console.log('>>>>>> ' + JSON.stringify(result, null, 4));
}
});
And here is the error :
/home/ankur/Private/Work/ps/node_modules/mongoose/lib/index.js:343
if (!this.modelSchemas[name]) {
^
TypeError: Cannot read property 'users' of undefined
at new Mongoose.model (/home/ankur/Private/Work/ps/node_modules/mongoose/lib/index.js:343:25)
at Object.<anonymous> (/home/ankur/Private/Work/ps/models_mongoose/Users.model.js:24: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 Function.Module.runMain (module.js:497:10)
at startup (node.js:119:16)
at node.js:929:3
Now my question is in this code I have hardly done anything extra, just configured the schema, created a new model on that schema and saved it. So where I missed the stuff?
Please shed some views.
Upvotes: 3
Views: 13129
Reputation: 13310
If you're coming from google - make sure your imports are correct. Webstorm will auto-import with import mongoose from 'mongoose';
but you need import * as mongoose from 'mongoose';
Upvotes: 1
Reputation: 2287
You have few errors. I've attached fully working example:
var mongoose = require("mongoose");
mongoose.connect('mongodb://localhost:27017/db_name'); //you need open to connection - this is an example of valid connection string
var Schema = mongoose.Schema;
var Users = new Schema({ // example from docs
_id : {
type : String,
require : true
},
username : {
type : String,
required : true
},
password : {
type : String,
required : true
}
});
var UsersModel = mongoose.model('users_test', Users); //remove 'new' keyword
var newUser = new UsersModel({"username":"A", "password":"V", "_id": 0}); // you also need here to define _id since, since you set it as required.
newUser.save(function(err, result){
if(err){
console.log('>>>>>> Error');
}else{
console.log('>>>>>> ' + JSON.stringify(result, null, 4));
}
});
To sum up:
connect
method on mongoose
objectnew Schema
constructor while creating modelnew
keyword from var UsersModel = mongoose.model(...)
_id
key with value, because without that you will get error that this value is missingUpvotes: 4
Reputation: 3340
You mixed up Schema and model.
var mongoose = require("mongoose");
var Schema = mongoose.Schema;
var Users = new Schema({ // add schema here!
_id : {
type : String,
require : true
},
username : {
type : String,
required : true
},
password : {
type : String,
required : true
}
});
var UsersModel = mongoose.model('users', Users); // remove new here!
var newUser = new UsersModel({"username":"A", "password":"V"});
Upvotes: 1