Error: mongoose-auto-increment has not been initialized
Error: mongoose-auto-increment has not been initialized
我正在创建一个 mongoose 模式,如下所示:
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});
当我尝试 运行 应用程序时,我收到一条错误消息:
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)
我正在这样初始化连接:
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);
});
}
这是我第一次使用 mongoose-aut-increment,因此我无法找出我的错误,有人可以帮忙吗?
对您的代码进行以下更改:
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);
adminSchema.plugin(autoIncrement.plugin, {
model: 'adminSchema',
field: 'admin_id',
startAt: 1,
incrementBy: 1
});
实际上,您并没有初始化自动递增的值。因此,现在通过分配一个默认值零(0),它肯定会每次递增 1。您可以根据需要更改默认值。
这件事发生在我身上,因为当执行这段代码时,mongoose 没有完全连接到数据库。
我将代码更改为以下并且有效。
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
});
const mongoose = require('mongoose');
Schema = mongoose.Schema,
autoIncrement = require('mongoose-auto-increment');
var connection = mongoose.createConnection("mongodb://localhost/mydatabase");
autoIncrement.initialize(connection);
constadminSchema = 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);
adminSchema.plugin(autoIncrement.plugin, {
model: 'adminSchema',
field: 'admin_id',
startAt: 1,
incrementBy: 1
});
我正在创建一个 mongoose 模式,如下所示:
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});
当我尝试 运行 应用程序时,我收到一条错误消息:
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)
我正在这样初始化连接:
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);
});
}
这是我第一次使用 mongoose-aut-increment,因此我无法找出我的错误,有人可以帮忙吗?
对您的代码进行以下更改:
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);
adminSchema.plugin(autoIncrement.plugin, {
model: 'adminSchema',
field: 'admin_id',
startAt: 1,
incrementBy: 1
});
实际上,您并没有初始化自动递增的值。因此,现在通过分配一个默认值零(0),它肯定会每次递增 1。您可以根据需要更改默认值。
这件事发生在我身上,因为当执行这段代码时,mongoose 没有完全连接到数据库。 我将代码更改为以下并且有效。
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
});
const mongoose = require('mongoose');
Schema = mongoose.Schema,
autoIncrement = require('mongoose-auto-increment');
var connection = mongoose.createConnection("mongodb://localhost/mydatabase");
autoIncrement.initialize(connection);
constadminSchema = 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);
adminSchema.plugin(autoIncrement.plugin, {
model: 'adminSchema',
field: 'admin_id',
startAt: 1,
incrementBy: 1
});