Merge pull request #3190 from strapi/fix/issue-3189

Cast arrays and build mongo where match with a $and
This commit is contained in:
Jim LAURIE 2019-04-26 16:03:50 +02:00 committed by GitHub
commit 541b00041a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 6 deletions

View File

@ -29,12 +29,10 @@ const buildQuery = ({ model, filters = {}, populate = [], aggregate = false } =
const buildSimpleQuery = ({ model, filters, populate }) => {
const { where = [] } = filters;
const wheres = where.reduce(
(acc, whereClause) => _.assign(acc, buildWhereClause(whereClause)),
{}
);
const wheres = where.map(buildWhereClause);
const findCriteria = wheres.length > 0 ? { $and: wheres } : {};
let query = model.find(wheres).populate(populate);
let query = model.find(findCriteria).populate(populate);
query = applyQueryParams({ query, filters });
return Object.assign(query, {

View File

@ -106,7 +106,13 @@ const buildQuery = ({ model, filters = {}, ...rest }) => {
});
const { type } = _.get(assocModel, ['attributes', attribute], {});
return { field, operator, value: castValueToType({ type, value }) };
// cast value or array of values
const castedValue = Array.isArray(value)
? value.map(val => castValueToType({ type, value: val }))
: castValueToType({ type, value: value });
return { field, operator, value: castedValue };
});
}