Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

128
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda