JavaScript-尝试过滤数据
谢谢您的回答,问题已经解决了像“这样的SQLJavaScript-尝试过滤数据,javascript,api,express,Javascript,Api,Express,谢谢您的回答,问题已经解决了像“这样的SQL正在查找空值 因此,如果请求中没有title,则需要向DB发送不同的WHERE子句 //Finds All of the User's Cards, and allows Searching by Title exports.findMyCards2 = (req, res) => { const { page, size, title, authorid } = req.query; const { limit, offset } =
正在查找空值
因此,如果请求中没有title
,则需要向DB发送不同的WHERE子句
//Finds All of the User's Cards, and allows Searching by Title
exports.findMyCards2 = (req, res) => {
const { page, size, title, authorid } = req.query;
const { limit, offset } = getPagination(page, size);
// Your default query
let query = {
authorid: { [Op.like]: `%${authorid}%` },
title: { [Op.like]: `%${title}%`}
}
// Query if the title is undefined (Remove the a title criteria of the WHERE)
if(!title){
query = {
authorid: { [Op.like]: `%${authorid}%` }
}
}
Card.findAndCountAll({
limit,
offset,
where: query // Then use it here
})
.then(data => {
const response = getPagingData(data, page, limit);
res.send(response);
})
.catch(err => {
res.status(500).send({
message:
err.message || "Some error occurred while retrieving Cards."
});
});
};
闻起来像是一个代码请求。两个都可以,但一个都不能。我想我遗漏了一些非常简单的东西。你读过我发布的任何东西吗?你需要一个if
语句来检查title
是否为空。你试过什么吗?我想)的结尾处,标题将处理该部分。这在我看来是正确的,但如果我只有作者,它仍然不起作用。我仍然得不到任何结果。如果没有标题,你能把标题记录下来吗?它是空字符串还是未定义的?如果是的话。。。只要改变条件;)当(!title){
will完成时,它显示为未定义。这似乎成功了!我正在测试它,并且一切正常。我真的很感谢你的帮助,我为没有在介绍中更清楚表示歉意。非常感谢!