szokodiakos / typegoose

Typegoose - Define Mongoose models using TypeScript classes.
MIT License
1.22k stars 135 forks source link

Error using @plugin(): First param to `schema.plugin()` must be a function, got "object" #172

Closed edg-l closed 5 years ago

edg-l commented 6 years ago
import * as uniqueValidator from "mongoose-unique-validator";

@plugin(uniqueValidator)
export class MyClass extends Typegoose {
...
}

I get this error:

Error: First param to `schema.plugin()` must be a function, got "object"
    at Schema.plugin (C:\Users\redacted\Desktop\projects\redacted\node_modules\mongoose\lib\schema.js:1184:11)
    at _.forEach (C:\Users\redacted\Desktop\projects\redacted\node_modules\typegoose\src\typegoose.ts:100:13)
    at arrayEach (C:\Users\redacted\Desktop\projects\redacted\node_modules\lodash\lodash.js:516:11)
    at Function.forEach (C:\Users\redacted\Desktop\projects\redacted\node_modules\lodash\lodash.js:9342:14)
    at User.buildSchema (C:\Users\redacted\Desktop\projects\redacted\node_modules\typegoose\src\typegoose.ts:99:9)
    at User.setModelForClass (C:\Users\redacted\Desktop\projects\redacted\node_modules\typegoose\src\typegoose.ts:38:20)
    at User.getModelForClass (C:\Users\redacted\Desktop\projects\redacted\node_modules\typegoose\src\typegoose.ts:28:12)
    at Object.<anonymous> (C:\Users\redacted\Desktop\projects\redacted\src\schemas\redacted.ts:55:37)
    at Module._compile (internal/modules/cjs/loader.js:689:30)
    at Module.m._compile (C:\Users\redacted\Desktop\projects\redacted\node_modules\ts-node\src\index.ts:439:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:700:10)
    at Object.require.extensions.(anonymous function) [as .ts] (C:\Users\redacted\Desktop\projects\redacted\node_modules\ts-node\src\index.ts:442:12)
    at Module.load (internal/modules/cjs/loader.js:599:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:538:12)
    at Function.Module._load (internal/modules/cjs/loader.js:530:3)
    at Module.require (internal/modules/cjs/loader.js:637:17)

uniqueValidator is actually a function:

(alias) function uniqueValidator(schema: Schema, options?: any): void
hasezoey commented 5 years ago

is this still a problem in the latest typegoose? (5.9.0)

hasezoey commented 5 years ago

@Ben305 this can be closed as "stale"

if someone still has this problem, please feel free to open a new issue at the new repo

DonsWayo commented 5 years ago

I have the same problem Error: First param to schema.plugin() must be a function, got "object"

@staticMethod static findOrCreate(condition: DocumentType<User>): Promise<{ doc: DocumentType<User>, created: boolean }> { const find = User.findOrCreate (condition).then((data) => { console.log(data); return data; }).catch ((error) => { return error; }); return find; }

hasezoey commented 5 years ago

@DonsWayo what plugin do you use and how do you apply it (@plugin)

DonsWayo commented 5 years ago

@hasezoey I have this class, I upgrade to the new repository and make the breaking changes, this is my model that make the problem

import { prop, getModelForClass, plugin, instanceMethod, staticMethod, DocumentType} from '@typegoose/typegoose';
import * as mongoose from 'mongoose';
import * as findOrCreate from 'mongoose-findorcreate';
import bcrypt from "bcryptjs";

@plugin(findOrCreate)
class User {

  @prop({ required: false })
  name!: string;

  @prop({ required: true })
  email!: string;

  @prop({ required: true })
  role!: string;

  @prop({ required: true })
  password!: string;

  @prop({select: false})
  __v?: number;

  @instanceMethod
  hashPassword(this: DocumentType<User>) {
    this.password = bcrypt.hashSync(this.password, 8);
  }
  @instanceMethod
  checkIfUnencryptedPasswordIsValid(unencryptedPassword: string) {
    console.log(this.password)
    console.log(unencryptedPassword)
    return bcrypt.compareSync(unencryptedPassword, this.password); 
  }

  @staticMethod
  static findOrCreate(condition: DocumentType<User>): 
  Promise<{ doc: DocumentType<User>,created: boolean }> {
       const find = User.findOrCreate (condition).then((data) => {
        return data;
    }).catch ((error) => {
        return error;
    });
      return find;
  }
}

const UserModel = getModelForClass(User);

export default UserModel;
hasezoey commented 5 years ago

@DonsWayo please reformat your last comment https://guides.github.com/features/mastering-markdown/


static findOrCreate(condition: DocumentType): Promise<{ doc: DocumentType, created: boolean }> > { const find = User.findOrCreate (condition).then((data) => { console.log(data); return data; }).catch ((error) => { return error; }); return find; } }

why do you have a plugin AND a mehtod that defines it?

and because typegoose uses this plugin for the tests, look here for infomation of how to use it

DonsWayo commented 5 years ago

In previos old version this works fine without problem, I try the solution of the file that you mentioned and still have the same issue.

hasezoey commented 5 years ago

@DonsWayo if you still have this problem (what you probably have), could you please open a new issue on the new repo / or ask again in discord?

ondrej-langr commented 3 years ago

For anyone stumbling upon this like I did - there is problem in import. I think its something to do with my tsconfig.json and target version but im not sure and I dont feel like investigating more since I've found the solution.

Which is in import and which has to be like: import { default as autopopulate } from "mongoose-autopopulate"; (For me its autopopulate plugin - but im sure it will work with other ones too).

There may be problem when importing like * as something because that something will be now object which looks like { default: [Function: somePluginSlashFunctionName] }. It has to do something with how that plugin exports are structured and my actual project and as I said: thats all i can say 'bout it.

Coding ahoy!

hasezoey commented 3 years ago

@daichi-ranguru the tsconfig option that is probably causing this is esModuleInterop

PS: this is the old repo / project