繁体   English   中英

在 sequelize 中使用带或条件的 where 子句

[英]Use where clause with or condition in sequelize

我正在尝试在 sequelize 中使用或条件来表示查询,例如,

SELECT * FROM table WHERE title = "title" OR genre = "genre"

但是,这是我已经尝试过的

const checkTitle= await User.findOne({
        where: {
            $or: [
                { title: req.body.title },
                { genre: req.body.genre }
            ]
        }
    }).catch((err) => console.log(err.message));

但它一直给我一个错误,说无效值

Invalid value { title: 'title' }

我想知道如何在续集中正确使用 OR 条件?

您需要在代码中的标题和流派之后放置大括号。 代码应该是这样的:

const checkTitle= await User.findOne({
        where: {
            $or: [
                 title: {req.body.title },
                 genre: {req.body.genre }
            ]
        }
    }).catch((err) => console.log(err.message));

最新版本的 sequelize 修改了逻辑运算的语法。 为了

续集 >= 4.12

const checkTitle= await User.findOne({
        where: {
            [Op.or]: [
                title: {req.body.title },
                description: {req.body.genre }
            ]
        }
    }).catch((err) => console.log(err.message));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM