Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: mongoose-auto-increment has not been initialized

I am creating a mongoose schema as follows :

var adminSchema = new mongoose.Schema({
email:{type:String,unique:true,required:true},
passwordHash:{type:String},
isActivated:{type:Boolean},
admin_id:{type:Number,unique:true}
});

adminSchema.plugin(autoIncrement.plugin(),{model:'adminSchema',
field:'admin_id',
startAt:1,
incrementBy:1});

When i try running the application, i am getting an error saying this:

 if (!counterSchema || !IdentityCounter) throw new Error("mongoose-auto-increment has not been initialized");
                                      ^

Error: mongoose-auto-increment has not been initialized
at Object.exports.plugin (/Users/kumar/WebstormProjects/code-master/node_modules/mongoose-auto-increment/index.js:36:49)
at Object.<anonymous> (/Users/kumar/WebstormProjects/code-master/models/admin.js:22:34)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)
at Module.require (module.js:366:17)
at require (module.js:385:17)
at Object.global.App.require (/Users/kumar/WebstormProjects/code-master/app.js:23:16)
at Object.global.App.model (/Users/kumar/WebstormProjects/code-master/app.js:32:21)
at init (/Users/kumar/WebstormProjects/code-master/app/initializers/passport.js:6:15)
at Object.<anonymous> (/Users/kumar/WebstormProjects/code-master/app.js:66:44)
at Module._compile (module.js:435:26)
at Object.Module._extensions..js (module.js:442:10)
at Module.load (module.js:356:32)
at Function.Module._load (module.js:311:12)

i am initiliazing the connection like this :

var mongoose = require('mongoose');
var autoIncrement = require('mongoose-auto-increment');

 function connect(connectionString){
mongoose.connect(connectionString);

var db = mongoose.connection;
autoIncrement.initialize(db);
db.on('error',console.error.bind(console,"Connection error"));
db.once('open',function callback(){
   console.log("Connection established via :"+connectionString);
});

}

this is my first shot at mongoose-aut-increment, hence i am not able to figure out my mistake, can someone please help?

like image 676
kumarD Avatar asked Feb 08 '23 13:02

kumarD


1 Answers

This happened with me because when this code was executed, mongoose was not fully connected to database. I changed the code to following and it worked.

var mongoose = require('mongoose');

var adminSchema = new mongoose.Schema({
  email: {type: String, unique: true, required: true},
  passwordHash: {type: String},
  isActivated: {type: Boolean},
  admin_id: {type: Number, default: 0, unique: true}
});

module.exports = mongoose.model('adminSchema', adminSchema);
autoIncrement.initialize(mongoose.connection); // This is important. You can remove initialization in different file
adminSchema.plugin(autoIncrement.plugin, {
  model: 'adminSchema',
  field: 'admin_id',
  startAt: 1,
  incrementBy: 1
});
like image 174
Garbage Avatar answered Feb 12 '23 09:02

Garbage