'Make an object with two keys with the same name
I have a numericFilter function that uses both price and rating and can use both rating and price at the same time. But can't do two prices at a time, for example price>120 && price<300 because when it detects that the key by the name of price is called it overwrites the price property.
I tried using a second object and doing a queryObject += secondObject but that makes two separate objects when i want a single one.
The queryObject when there is both price and rating filter is: { price: { '$gt': 150 }, rating: { '$gt': 4 } } I was thinking that the way the object should look if i had multiple price filters should be { price: { '$gt': 150 }, price: { '$lt': 300 } } or { price: { '$gt': 150, '$lt':300 } }
Here is my original code that replaces the price key when it detects there is a new one:
const { featured, company, name, sort, props, numericFilters, all } =
req.query;
const queryObject = {};
if (numericFilters) {
const operatorMap = {
">": "$gt",
">=": "$gte",
"=": "$eq",
"<": "$lt",
"<=": "$lte",
};
const regEx = /\b(<|>|>=|=|<|<=)\b/g;
let filters = numericFilters.replace(
regEx,
(match) => `-${operatorMap[match]}-`
);
const options = ["price", "rating"];
filters = filters.split(",").forEach((item) => {
const [field, operator, value] = item.split("-");
if (options.includes(field)) {
queryObject[field] = { [operator]: Number(value) };
}
});
}
let result = Product.find(queryObject);
Here is my attempts that failed:
const { featured, company, name, sort, props, numericFilters, all } =
req.query;
const queryObject = {};
if (numericFilters) {
const operatorMap = {
">": "$gt",
">=": "$gte",
"=": "$eq",
"<": "$lt",
"<=": "$lte",
};
const regEx = /\b(<|>|>=|=|<|<=)\b/g;
let filters = numericFilters.replace(
regEx,
(match) => `-${operatorMap[match]}-`
);
const options = ["price", "rating"];
var arrayObject = [];
var secondObject = {};
filters = filters.split(",").forEach((item) => {
const [field, operator, value] = item.split("-");
if (options.includes(field)) {
if (queryObject[field] != null) {
//arrayObject[0] = queryObject[field];
//arrayObject[1] = { [operator]: Number(value) };
//Didnt work "CastError: Cast to Number failed for value "{ '$gt': 150 }" (type Object) at path "price" for model "Product""
//secondObject[field] = { [operator]: Number(value) };
//queryObject[field] += secondObject; //CastError: Cast to Number failed for value "[object Object][object Object]" (type string) at path "price" for model "Product"
//secondObject[field] = { [operator]: Number(value) };
//queryObject[field] = secondObject; //CastError: Cast to Number failed for value "{ price: { '$lt': 300 } }" (type Object) at path "price" for model "Product"
} else {
queryObject[field] = { [operator]: Number(value) };
}
}
});
console.log(queryObject);
}
let result = Product.find(queryObject);
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
