Filter issues in lib API QUERY PARAMS

Asked

Viewed 93 times

0

I am using this lib to apply filters

api-query-params

I need to perform the filter for this entity;

'use strict'

const mongoose = require('mongoose');
const Schema = mongoose.Schema;


const schema = new Schema({
    id: {
        type: String,
        trim: true
    },
    name: {
        type: String,
        trim: true

    },
    description: {
        type: String,
        trim: true
    },
    restaurantId: {
        type: String,
        trim: true
    },
    price: {
        type: Number,
        trim: true
    },

})

module.exports = mongoose.model('Menu', schema);

Through the lib documentation API QUERY PARAMS can create this method;

exports.list_all_dataProviders = async (req, res) => {
     const { filter, skip, limit, sort, projection } = aqp(req.query);

    Menus
      .find(filter)
      .skip(skip)
      .limit(limit)
      .sort(sort)
      .select(projection)
      .exec(async (err, result) => {

        if (err) {
          return res.status(500).jsonp({message:"There was an internal error listing all the providers " + err});
        }
        let count = await Menus.find().count()
        res.status(200).jsonp({
          size: limit,
          page: skip,
          total: count,
          data: result
        });
      });
  };

And I took the following test at the Postman;

http://localhost:3000/menu?price=14.9

http://localhost:3000/menu?price=4.9

http://localhost:3000/menu?price=6.9

And it stuck perfectly, but I took this test and I didn’t succeed;

It failed to perform any filter for the Description field http://localhost:3000/menu?description=Coberto

And for the name field only managed to get the first record.

What’s wrong is my method, I need to correct.

  • It seems strange to talk "lib API ...", usually use one or the other

  • And which lib is used for my case?

  • That’s not what I meant... lib is nickname for library which is, in English, library, which is "a real implementation of the rules", already API, "It’s the way your code relates to a library". It is unusual to use both terms together (not necessarily wrong). Difference between the terms

  • @Guilhermecostamilam Thanks for clarifying, but in the end you have any suggestions to help me in my problem according to the posting? Please.

1 answer

1


You did not present the data you have in your database, but by subtended in your question you want to search the items that contain the text that was passed, so you should use a regular expression indicating this. Your URL will change to:

http://localhost:3000/menu?description=/coberto/i

The modifier i determines that the search will ignore the case of his string.

  • I would like to ask one more question if it is not a problem, there is possibility to use the URL without having to put the i? Is there a way to change my method so you don’t need to use the modifier i in the URL ?

  • 1

    @wladyband See that this module you installed serves precisely to facilitate. If you take the i your search will consider the case of letters. So you have to decide in reality whether or not it is worth using a facilitator since if you want a different composition you will have to define it in the logic of your service.

  • All right, I get it, thank you very much.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.