【发布时间】:2020-06-28 10:57:40
【问题描述】:
我无法修改过滤参数
String.replace()
我可以从作为对象的 URL 中获取过滤键,但它从我这里得到了严重的错误。
过滤:{{URL}}/api/v1/bootcamps?averageCost[lt]=10000
当前格式: { averageCost: { lt: '10000' } }
右从at: { averageCost: { $lt: '10000' } }
所以我尝试将其转换为字符串并替换该值。但该值可以是:lt, lte, gt, gte, in 但它有一些问题,因为 .replace() 方法之后的行没有执行,当然catch 块包含错误...
我的代码sn-p:
try {
console.log(req.query);
const queryStr = JSON.stringify(req.query);
console.log(queryStr); //thats the last thing I get
queryStr = queryStr.replace(
/\b(gt|gte|lt|lte|in)\b/g,
match => `$${match}`
);
console.log(queryStr); // I dont get this
const bootcamps = await Bootcamp.find();
res.status(200).json({
succes: true,
count: bootcamps.length,
data: bootcamps
});
} catch (err) {
return res.status(404).json({ succes: false });
}
【问题讨论】: