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

127
Views
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 answers
Answer question

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 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!