Sails.js : Waterline OR filter not working

417 views Asked by At

I am developing a sails.js app using default waterline.

I have an array of categories and locations as shown below.

var categoryOrFilter = [1,2,3];
var locationsOrFilter = [8,9,10];

I am trying to write a where on my model which will filter the model items belonging to at least one of the categories in the categoryOrFilter AND at least one of the locations in the locationsOrFilter.

My model is as shown below.

attributes: {

        category: {
            model: 'category'
        },

        location: {
            model: 'location'
        }

}

I've written my query as shown below.

 Model.find()
        .where({
            or: [{
                "category": categoryOrFilter
            }]
        })
        .where({
            or: [{
                "location": locationsOrFilter
            }]
        })
        .exec(function (err, modelItem) {
             //Some logic
        });

My categoryOrFilter values are not being considers. My locationOrFilter values work perfect.

What am I doing wrong?

1

There are 1 answers

1
Ofer Herman On BEST ANSWER

The 2nd where and the 'or' are not needed the default operator between criteria parts is AND and for array elements in a single criteria part it's IN

Model.find()
    .where({
            "category": categoryOrFilter,
            "location": locationsOrFilter
    })
    .exec(function (err, modelItem) {
         // Some logic
    });