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

299
Views
La propiedad 'byName' no existe en el error de tipo '{}'

Entonces, estaba probando mecanografiado, en el sitio web de mongoose pude ver una guía para mecanografiado, para analizar el código, copié un fragmento de la documentación oficial .

 import { Document, Model, Query, Schema, connect, model } from 'mongoose'; interface Project { name: string; stars: number; } const schema = new Schema<Project>({ name: { type: String, required: true }, stars: { type: Number, required: true } }); // Query helpers should return `Query<any, Document<DocType>> & ProjectQueryHelpers` // to enable chaining. interface ProjectQueryHelpers { byName(name: string): Query<any, Document<Project>> & ProjectQueryHelpers; } // The error is on the below line, it says Property 'byName' does not exist on type '{}' schema.query.byName = function(name): Query<any, Document<Project>> & ProjectQueryHelpers { return this.find({ name: name }); }; // 2nd param to `model()` is the Model class to return. const ProjectModel = model<Project, Model<Project, ProjectQueryHelpers>>('Project', schema); run().catch(err => console.log(err)); async function run(): Promise<void> { await connect('mongodb://localhost:27017/test'); // Equivalent to `ProjectModel.find({ stars: { $gt: 1000 }, name: 'mongoose' })` await ProjectModel.find().where('stars').gt(1000).byName('mongoose'); }

No estoy seguro de por qué ha ocurrido este error, ¿alguien puede ayudarme? Al inspeccionar el objeto de esquema, pude ver que la consulta era {}. Pero ¿por qué así?

about 4 years ago · Santiago Gelvez
1 answers
Answer question

0

Creo que su problema es una versión mecanografiada, estoy usando v4.5.4 y todo está bien.

prueba esto, por favor:

 (schema as Schema<Project> & { query: ProjectQueryHelpers }).query.byName = function ( name: string ): Query<any, Document<Project>> & ProjectQueryHelpers { return this.find({ name: name }); };
about 4 years ago · Santiago Gelvez 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!