
import mongoose from "mongoose";
import autoIncrement from "mongoose-auto-increment";
const userSchema = mongoose.Schema({
name: String,
username: String,
email: String,
phone: String
})
autoIncrement.initialize(mongoose.connection);
userSchema.plugin(autoIncrement.plugin, 'user'); //error occurs in this line, when I comment this line, rest work fine.
const user = mongoose.model('user', userSchema);
export default user;
In the given code, when I comment the line number 12 which is as follow.
userSchema.plugin(autoIncrement.plugin, 'user');
Then it work fine but when I try to go with `userSchema.plugin(autoIncrement.plugin, 'user');`.\
It gives the error which can clearly be seen in the screenshot under terminal section.
Error message is as follow.
MongooseError: Model.findOne() no longer accepts a callback
at Function.findOne (D:\personal project\rough\CRUD\server\node_modules\mongoose\lib\model.js:2129:11)
at exports.plugin (D:\personal project\rough\CRUD\server\node_modules\mongoose-auto-increment\index.js:73:19)
at Schema.plugin (D:\personal project\rough\CRUD\server\node_modules\mongoose\lib\schema.js:1884:3)
at file:///D:/personal%20project/rough/CRUD/server/schema/user-schema.js:12:12
at ModuleJob.run (node:internal/modules/esm/module_job:194:25)
Node.js v19.1.0
[nodemon] app crashed - waiting for file changes before starting...
- I uninstalled and reinstalled the
mongoose-auto-increment. - I have check this
Function.findOne (node_modules\mongoose\lib\model.js:2129:11)but not find anything which I need to modify.
I have the same problem when migrate old project to mongodb 6.0.
There are more problem with it
Model.prototype.save() no longer accepts a callback.I just make my project run, not assure the function right.
I have find the origin repo here.
The Line 73 used the
findOnefunction. I think that's the problem. But the lib was no longer maintained. Maybe we should change to other libs or import the code and fix it.I am import it to local file and fix it.
package.jsonadd dependency.
mongoose-auto-increment.js.require(), depends on themongoose-auto-increment.jslocation.That's all.