我正在尝试实现这个 numericFilter;
if(numericFilters){
const mapOperators ={
'>':'>',
'>=':'>e',
'=':'&eq',
'<':'<',
'<=':'<e',
}
const regEx = /\b(>|>=|=|<|<=)\b/g;
let filter = numericFilters.replace(regEx , (match)=>
`-${mapOperators[match]}-`
)
const options = ['price','rating']
filter = filter.split(',').forEach((element) => {
const [field,operator,value] = element.split('-')
if(options.includes(field)){
console.log([field,operator,value]);
queryObject[field] = { [operator]: Number(value) }
console.log(queryObject);
}
});
}
我得到这个 CastError :AssertionError [ERR_ASSERTION]: The expression evaluated to a falsy value 消息格式:未定义, 字符串值:
"{ '>': 40 }"
,
种类:'数字',
价值:{'&gt':40},
路径:'价格',
我似乎找不到解决这个错误的方法
它尝试从 mongoose 查询文档站点实现它,但仍然没有工作
if(options.includes(field)){
console.log([field,operator,value]);
queryObject[field] = { [operator]:{notInSchema: Number(value) }}
console.log(queryObject);
}
本节的预期结果是根据值从模式中过滤掉 ['price','rating'] 传递给数字过滤器:
这是我的模式:
new mongoose.Schema({
name: {
type: String,
required: [true, 'product name must be provided'],
},
price: {
type: Number,
required: [true, 'product price must be provided'],
},
featured: {
type: Boolean,
default: false,
},
rating: {
type: Number,
default: 4.5,
},
createdAt: {
type: Date,
default: Date.now(),
}