Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

126
Vistas
Search api in node.js

this piece of coded works properly but I'm a beginner so I need to understand the code. plz anyone can help me with the explanation.

class ApiFeatures{
    constructor(query,querystr){
        this.query = query;
        this.querystr = querystr;
    }

    search(){
        const keyword = this.querystr.keyword ?{
            name:{
                $regex : this.querystr.keyword,
                $options:"i"
            }
        }:{}

        this.query = this.query.find({...keyword});
        return this;
    }
}

module.exports = ApiFeatures;

Can Anyone plz explain why we did this.query = query and this.querystr = querystr in constructor

exports.getproducts = async (req, res, next) =>{

    const ApiFeatures = new APIFeatutres(PRODUCT.find(), req.query).search()
    const products = await ApiFeatures.query;
    res.status(200).json({
        success:true,
        Count: products.length,
        products
    })
}

here also const products = await ApiFeatures.query is mysterious.

about 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Javascript classes have properties (like in OOP languages if you've already worked with any). But we don't need to explicitly declare those properties, you just assign them in the constructor and they exist. So when you do this.query = query and this.querystr = querystr, we're assigning query to a variable named query that exists inside the ApiFeatures class, and same of querystr. Tis way, when we call this.querystr in the search function (or anywhere in the ApiFeature class), we will get the variable that we passed to the constructor.

Then, in the search function we have this: this.query = this.query.find({...keyword});, we assign the promise (the result of the this.query.find({...keyword})) to this.query. So when you do Apifeatures.query later, we're accessing this promise and awaiting it, so we can get the result inside products.

An example:

    const ApiFeatures = new APIFeatutres(PRODUCT.find(), {keyword: 'something'}).search()
    //The {keyword: 'something'} querystr will be used by search function to search all occurrences of products that have a name that matches 'something'.

    //The promise of this query is then stored in `this.query`.

    // Then, we await this promise and get the result in products, which might return [{name: 'something1'}, ...]
    const products = await ApiFeatures.query;

(But, some things are not right with this code, the way It makes a whole object for one request, that we pass the query as a parameter is very PRODUCT.find() as a query and then calling it query.find, basically doing PRODUCT.find().find()

As jonrsharpe said above, this whole thing could be done with one simple function.)

about 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda