Closed edg-l closed 5 years ago
is this still a problem in the latest typegoose? (5.9.0)
@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
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; }
@DonsWayo what plugin do you use and how do you apply it (@plugin
)
@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;
@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
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.
@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?
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!
@daichi-ranguru the tsconfig option that is probably causing this is esModuleInterop
PS: this is the old repo / project
I get this error:
uniqueValidator is actually a function: