Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

214
Views
Cree métodos de esquema de mangosta usando TypeScript

Intento crear el método hashPassword para el esquema de usuario.

 schema.method("hashPassword", function (): void { const salt = bcrypt.genSaltSync(10); const hash = bcrypt.hashSync(this.password, salt); this.password = hash; });

Y obtiene un error Property 'password' does not exist on type 'Document<any>'. en contraseña

Aquí está mi archivo

 import mongoose, { Schema, Document } from "mongoose"; import bcrypt from "bcryptjs"; /** * This interface should be the same as JWTPayload declared in types/global.d.ts file */ export interface IUser extends Document { name: string; email: string; username: string; password: string; confirmed: boolean; hashPassword: () => void; checkPassword: (password: string) => boolean; } // User schema const schema = new Schema( { name: { type: String, required: true, }, email: { type: String, required: true, }, username: { type: String, required: true, }, password: { type: String, required: true, }, confirmed: { type: Boolean, default: false, }, }, { timestamps: true } ); schema.method("hashPassword", function (): void { const salt = bcrypt.genSaltSync(10); const hash = bcrypt.hashSync(this.password, salt); this.password = hash; }); // User model export const User = mongoose.model<IUser>("User", schema, "users");
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

En el punto en el que define el método, el objeto schema no sabe que es el Schema para un IUser y no cualquier Document . Debe establecer el tipo genérico para el Schema cuando lo crea: new Schema<IUser>( ... ) .

over 4 years ago · Santiago Trujillo Report

0

Como lo sugirió uno de los colaboradores de mongoose, podemos usar la siguiente forma para crear métodos de instancia:

 const schema = new Schema<ITestModel, Model<ITestModel, {}, InstanceMethods>> // InstanceMethods would be the interface on which we would define the methods schema.methods.methodName = function() {} const Model = model<ITestModel, Model<ITestModel, {}, InstanceMethods>>("testModel", ModelSchema) const modelInstance = new Model(); modelInstance.methodName() // works

enlace: https://github.com/Automattic/mongoose/issues/10358#issuecomment-861779692

over 4 years ago · Santiago Trujillo Report

0

Debe declarar una interfaz que amplíe Model así:

 interface IUser {...} interface IUserInstanceCreation extends Model<IUser> {}

luego declara tu esquema;

 const userSchema = new Schema<IUser, IUserInstanceCreation, IUser>({...})

Esto también garantizaría que el esquema siga los atributos en IUser.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!