mongoose-sequence icon indicating copy to clipboard operation
mongoose-sequence copied to clipboard

Error Counter already defined for field "userId_counter" in Next.js typescript

Open harloom opened this issue 3 years ago • 4 comments

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;

harloom avatar Feb 09 '21 13:02 harloom

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. (E:\Project\tcc-next-new.next\server\pages\api\user\register.js:94:10) at Module._compile (internal/modules/cjs/loader.js:1137:30) at Object.Module._extensions..js (internal/modules/cjs/loader.js:1157:10) at Module.load (internal/modules/cjs/loader.js:985:32) at Function.Module._load (internal/modules/cjs/loader.js:878:14) at Module.require (internal/modules/cjs/loader.js:1025:19) at require (internal/modules/cjs/helpers.js:72:18) at DevServer.handleApiRequest (E:\Project\tcc-next-new\node_modules\next\dist\next-server\server\next-server.js:64:181)

harloom avatar Feb 09 '21 13:02 harloom

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

ramiel avatar Feb 16 '21 21:02 ramiel

Hey, make sure to drop the counter collection and try again - worked for me

cosmin-techoff avatar Apr 09 '21 16:04 cosmin-techoff

@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

eduzera avatar Jun 02 '21 19:06 eduzera