[英]sequelize dynamic construct where with Op.or javascript
const type = TYPE.get(type_name) || '[Op.or]: [{type: 1}, {type: 2}]';
await someTable.findAndCountAll({
where: { user_id, type },
attributes: [
'title',
['description', 'desc'],
['createdAt', 'created'],
['updatedAt', 'last_modified'],
],
});
所以,我正在嘗試為 where 條件傳遞類型。 如果 TYPE.get(type_name) 返回一個值,則使用該值作為類型,否則使用 Op.or 操作來包含類型 1 和 2。但是,我試圖讓它工作的方式不起作用。 目前,如果 TYPE.get(type_name) 返回一個值,它可以工作。 如果它沒有返回值,則 where 條件被替換為
type='[Op.or]: [{type: 1}, {type: 2}]'
這不是我想要的。 有誰知道如何正確完成這項工作?
我能夠弄清楚。 需要執行以下操作:
const where = type
? { user_id, status: 1, type}
: { user_id, status: 1, [Op.or]: [{ type: 1 }, { type: 2 }] };
await someTable.findAndCountAll({
where: where,
attributes: [
'title',
['description', 'desc'],
['createdAt', 'created'],
['updatedAt', 'last_modified'],
],
});
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.