Skip to content

如何根据前端给过来的参数判断查询条件 #14

Open
@EasonYou

Description

@EasonYou

比如前端传一个为 name 的参数,如果为空,则查询全部,不为空则查询所有

Model.findAll({
  where: {
    name: query.name
  }
})

我该如何判断这里query.name如果为空,就不要有name这个条件?
试过三目运算符置为 undefined,可是出来的查询条件是 where name=null

还是说我只能把where拎出来,用if-else判断来加入条件,再做查询呢

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions