adonis-mongodb icon indicating copy to clipboard operation
adonis-mongodb copied to clipboard

Collection name must be a String

Open q8tywolf opened this issue 2 years ago • 1 comments

Hello Am getting this error

It would be great to add to the readme how to define models with BaseModel

my model is

export default class User extends BaseModel {

  email: string
  password: string
  rememberMeToken: string | null
  createdAt: Date
  updatedAt: Date

  status: StatusEnum = StatusEnum.Pending;
 

  static async hashPassword(user: User) {
    if (user.$dirty.password) {
      user.password = await Hash.make(user.password)
    }
  }

  static async isEmailTaken(email: string, excludeUserId) {
    const user = await this.find({ email, _id: { $ne: excludeUserId } });
    return !!user;
  }
}

error thrown

    err: {
      "type": "MongoInvalidArgumentError",
      "message": "Collection name must be a String",
      "stack":
          MongoInvalidArgumentError: Collection name must be a String
              at checkCollectionName (/var/www/html/adonis/node_modules/mongodb/src/utils.ts:84:11)
              at new Collection (/var/www/html/adonis/node_modules/mongodb/src/collection.ts:174:24)
              at Db.collection (/var/www/html/adonis/node_modules/mongodb/src/db.ts:298:12)
              at Connection.collection (/var/www/html/adonis/node_modules/@zakodium/adonis-mongodb/src/Database/Connection.ts:121:15)
              at processTicksAndRejections (node:internal/process/task_queues:95:5)
              at Function.find (/var/www/html/adonis/node_modules/@zakodium/adonis-mongodb/src/Model/Model.ts:406:24)
              at Function.isEmailTaken (/var/www/html/adonis/app/Models/User.ts:53:18)

q8tywolf avatar Jun 08 '23 09:06 q8tywolf

Your definition of the user class seems correct and it runs without errors on my end if I replace this.find with this.query. I suspect that you are using an old version of the package.

targos avatar Jun 14 '23 07:06 targos