Problems with filter in lib API QUERY PARAMS

0

I'm using this lib to apply filters

api-query-params

I need to 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 API documentation 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 did the following tests on Postman;

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

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

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

And it took perfectly, but I did this test and I did not succeed;

It could not perform any filter for the description field http://localhost:3000/menu?description=Coberto

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

What's wrong is my method, I need to fix it.

    
asked by anonymous 17.08.2018 / 18:27

1 answer

1

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

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

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

    
21.08.2018 / 03:21