Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
harloom opened this issue Feb 9, 2021 · 4 comments

Comments

@harloom
Copy link

harloom commented Feb 9, 2021

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
Copy link
Author

harloom commented Feb 9, 2021

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)

@ramiel
Copy link
Owner

ramiel commented Feb 16, 2021

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

@cosmin-techoff
Copy link

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

@eduzera
Copy link

eduzera commented Jun 2, 2021

@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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants