`{where: 'raw query'}`支持被移除致过滤失效,求修复方案
解决
Support for {where: 'raw query'} has been removed 错误 问题根源
你的代码中,当单独传入descriptionId或locationId时,直接将变量赋值给where(比如{ where: descriptionId }),这种直接传递原始值而非键值对对象的写法,在新版本Sequelize中已被移除支持,这就是报错的核心原因。旧版本Sequelize允许这种简化写法,但更新后要求where必须是明确的查询条件对象。
修复方案
将所有where参数改为键值对对象格式,同时简化冗余的分支判断,通过动态构建查询条件来优化代码:
修改后的代码
async getAll(req, res) { const { descriptionId, locationId } = req.query; const where = {}; if (descriptionId) { where.descriptionId = descriptionId; } if (locationId) { where.locationId = locationId; } const photos = await Photo.findAll({ where }); return res.json(photos); }
代码说明
- 初始化空的
where对象,根据请求参数动态添加查询条件 - 无论是否有查询参数,
Photo.findAll({ where })都能正确执行:无参数时where为空对象,等价于查询所有数据 - 彻底避免了直接传递原始值给
where的写法,适配新版本Sequelize的要求
内容的提问来源于stack exchange,提问作者Nikita
相关产品推荐
相关产品推荐

