Skip to content

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

@harloom

Description

@harloom

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;

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions