mongoose-sequence
mongoose-sequence copied to clipboard
Error Counter already defined for field "userId_counter" in Next.js typescript
Hello i am build app with next.js typescript and mongoose
first time input to db is not problem. i close and run dev .i try create api i have message this error;
i try not use plugin . my code is fine
import mongoose ,{
Document, Model, Schema, model, DocumentQuery, PaginateModel
} from 'mongoose';
// plugin auto increment
const AutoIncrement = require('mongoose-sequence')(mongoose);
// plugin pagination
import paging from 'mongoose-paginate-v2';
export const RoleAccount = {
ADMIN: "ADMIN",
PERUSAHAAN: "PERUSAHAAN",
MAHASISWA: "MAHASISWA"
}
export interface IAccount extends Document {
/** Name of the User */
userId: string;
nama: string;
username: string;
email: string;
numberPhone: string;
emailVerifyAt: boolean;
photo_path: string;
password: string;
role: string;
}
interface IAccountModel extends Model<IAccount,typeof userQueryHelpers> {
}
interface IPagingAccountModel<IAccountModel> extends PaginateModel<IAccount> {
}
const accountSchema = new Schema({
userId: { type: Number, default : 0},
nama: { type: String, default: '', required: true },
username: { type: String, required: true },
email: { type: String,unique: true,default:'' },
numberPhone: { type: String, default: '' },
emailVerifyAt: { type: Boolean, default: false, required: true },
photo_path: { type: String, default: 'default', required: true },
password: { type: String, required: true },
role: { type: String, default: RoleAccount.MAHASISWA, required: true }
}, { timestamps: true });
let userQueryHelpers = {
hidenPassword(this : DocumentQuery<any, IAccount>){
return this.select('-password');
}
};
accountSchema.query = userQueryHelpers;
accountSchema.plugin(AutoIncrement,{id:'userId_counter',inc_field:'userId' });
accountSchema.plugin(paging);
const Account: IPagingAccountModel<IAccountModel> =model<IAccount, any>("account", accountSchema);
export default Account;
Error: Counter already defined for field "userId_counter"
at SequenceFactory.Sequence.getInstance (E:\Project\tcc-next-new\node_modules\mongoose-sequence\lib\sequence.js:99:51)
at Schema.plugin (E:\Project\tcc-next-new\node_modules\mongoose\lib\schema.js:1514:3)
at Module../src/models/Account.ts (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:328:15)
at webpack_require (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:23:31)
at Module../src/models/index.ts (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:347:66)
at webpack_require (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:23:31)
at Module../src/controllers/users/UserController.ts (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:105:65)
at webpack_require (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:23:31)
at Module../src/controllers/users/index.ts (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:219:73)
at webpack_require (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:23:31)
at Module../src/pages/api/user/register.ts (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:366:76)
at webpack_require (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:23:31)
at E:\Project\tcc-next-new.next\server\pages\api\user\register.js:91:18
at Object.
It depends where you define the models. They should be defined just once otherwise you'll have this problem. In which file you put your definitions? How are you importing this file? If you use it in a function (let's say in /pages/api/myapi.js), try to be sure to not call the definition in the function you export, but do it outside of that function
Hey, make sure to drop the counter collection and try again - worked for me
@harloom try to check if your collection exist. example:
if(!mongoose.models.account){
accountSchema.plugin(AutoIncrement,{id:'userId_counter',inc_field:'userId' });
}
ps: to do that you need to remove and create your account collection. ps2: this plugin create one collection called counters, you can check if this collection + field exists there.
i hope it's solve your problem