如果查询字符串中的字段为空,如何获取所有数据 Node 并使用 Postgres 进行 Sequelize [英] How get all data if field in query string is empty Node and Sequelize with Postgres
问题描述
我正在用 Node 学习 Sequelize,我有一个问题..
i'm studing Sequelize with Node, and i have a question..
我有以下带有参数的 URL:
I have the follow URL with params:
http://localhost:9000/api/get/Joao/""
第一个参数是name
,第二个参数是manager
,分别是Joao
和""
(i'我考虑像空一样)..
The first parameter is name
and the second is manager
, respectively Joao
and ""
(i'm considering like empty)..
我想要的是:如果第二个参数是 empty
,我想获取所有具有 name
Joao..
What i want is:
If the second parameter is empty
i want to get all data that has the name
Joao..
已经把undefined, null
放在Sequelize
的代码中,但是没有用..
Already put undefined, null
in the code of the Sequelize
but not working..
有人可以帮我吗?
我的Sequelize
代码:
getCubikMembers(req, res) {
return Cubik
.findAll({
where: {
name: req.params.name,
manager: null // what i put here to happen what i want?
}
})
.then(success => res.status(201).send(success))
.catch(err => res.status(err).send(err))
}
我的api:
app.get('/api/get/:name/:manager',CubikersController.getCubikMembers);
推荐答案
我必须这样做,但我必须将 ""
更改为 undefined
,我不这样做知道但Sequelize中的空值不同..
i got to do, but i have that to change the ""
to undefined
, i dont know but the value empty is different in Sequelize..
我的代码是:
where: {
name: req.params.name === 'undefined' ? {
[Op.ne]: 'undefined'
} : req.params.name,
manager: req.params.manager === 'undefined' ? {
[Op.ne]: 'undefined'
} : req.params.manager
},
像这样,如果某个字段是 undefined
,它会在过滤器中被忽略..
Like this if some field is undefined
it is ignored in the filter..
我希望能帮助到某人.
这篇关于如果查询字符串中的字段为空,如何获取所有数据 Node 并使用 Postgres 进行 Sequelize的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!