javascript - 如果使用空字符串传递,如何使 mongoose.find 不返回所有数据
问题描述
我在每个视频数据上都有标题名称,并编写了一个搜索 api,它将获取与标题名称匹配的视频数据
exports.findbytitle = (req, res) => {
let userPattern = new RegExp(req.body.title, "i");
Videos.find({ title: userPattern })
.then((video) => {
res.send({ success: true, video });
})
.catch((err) => {
res.send({ success: false, err });
});
};
这里的问题是如果userPattern
匹配我得到正确的视频,但如果userPattern
是空字符串示例:("title": ""
)我得到所有视频而不是错误。userPattern
如果作为空字符串传递,我应该进行哪些更改才能得到错误?
解决方案
如您所知,仅当标题为空时,为什么不在查询前添加条件?
if(!req.body.title) {
return res.send({ success: false, err: new Error("Not found")})
}
推荐阅读
- php - 在数据表中包含一个编辑按钮
- python - Python“键盘”模块以非root身份工作但不能在sudo(linux)中工作
- android - 启动协程生成器会导致挂起函数在新线程上运行吗?
- computer-vision - 如何在 Detectron2 中进行超参数调整
- flutter - 如何在颤动中有多个相同数据类型的流提供者?
- rpy2 - 在 rpy2 中使用 suppressMessages 仍然会打印消息
- pine-script - 在多个时间范围内同时使用 Fisher 变换
- python - 为什么这个程序调用了错误的方法?[Python, MultiTimer 库]
- java - 获取 415 内容不受支持 application/pkixcmp
- charts - 在条形图中的条形内绘制点和线