Javascript 中的一行 if/else 用于 Sequelize 查询
One line if/else in Javascript for Sequelize query
我的工作 if/else 看起来像这样:
async invoke(request, response) {
const { deleted } = request.query;
let users = [];
if (deleted === 'true') {
users = await User.findAndCountAll({
where: {
deletedAt: { [Op.not]: null }
}
});
} else if (deleted === 'false') {
users = await User.findAndCountAll({
where: {
deletedAt: null
}
});
}
return response.send(users);
}
我想把它变成一个衬垫,但问题是{已删除} return“未定义”和“真”,我需要它return“真”和“假”
异步调用(请求,响应){
const { 删除 } = request.query;
const deletedAt = deleted ? { [Op.not]: null } : null;
const users = await User.findAndCountAll({
where: {
deletedAt
}
});
return response.send(users);
}
假设 deleted
的唯一两个有效状态是 "true"
和 "false"
(奇怪的是它是一个字符串),那么由于findAndCountAll
调用的唯一区别是 deletedAt
的值,您可以使用条件运算符:
async invoke(request, response) {
const { deleted } = request.query;
let users = await User.findAndCountAll({
where: {
deletedAt: deleted === 'true' ? { [Op.not]: null } : null
// −−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
});
return response.send(users);
}
我的工作 if/else 看起来像这样:
async invoke(request, response) {
const { deleted } = request.query;
let users = [];
if (deleted === 'true') {
users = await User.findAndCountAll({
where: {
deletedAt: { [Op.not]: null }
}
});
} else if (deleted === 'false') {
users = await User.findAndCountAll({
where: {
deletedAt: null
}
});
}
return response.send(users);
}
我想把它变成一个衬垫,但问题是{已删除} return“未定义”和“真”,我需要它return“真”和“假”
异步调用(请求,响应){ const { 删除 } = request.query;
const deletedAt = deleted ? { [Op.not]: null } : null;
const users = await User.findAndCountAll({
where: {
deletedAt
}
});
return response.send(users);
}
假设 deleted
的唯一两个有效状态是 "true"
和 "false"
(奇怪的是它是一个字符串),那么由于findAndCountAll
调用的唯一区别是 deletedAt
的值,您可以使用条件运算符:
async invoke(request, response) {
const { deleted } = request.query;
let users = await User.findAndCountAll({
where: {
deletedAt: deleted === 'true' ? { [Op.not]: null } : null
// −−−−−−−−−−−−−−−−−−−−^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
}
});
return response.send(users);
}