用Sequelize查询

时间:2019-08-02 04:09:45

标签: node.js sequelize.js

我正在像这样进行续集查询:

const users = await User.findAll({
    attributes: ['id', Sequelize.literal(`"firstName" || ' ' || "lastName" as name`)],
    where: { status: `[${USER_STATUS.ACTIVE}]`, organizationId: orgId }
  });

我遇到问题

  

“ organization_users”。“状态” ='“ [1]”'

我希望它发出这样的查询:

  

“ organization_users”。“状态” ='[1]'

,不带双引号。谢谢。我尝试了很多方法,但是没有用。任何帮助表示赞赏!

我对stackoverflow给出了答案:

where: { status: { [Op.contains]: Sequelize.literal(`[${USER_STATUS.ACTIVE}]`) }, organizationId: orgId }

但查询为

  

“ organization_users”。“状态” @> [1]

也许这是续集的错误?

2 个答案:

答案 0 :(得分:0)

您可以尝试将A().a()的值预先存储在变量中,然后将该变量作为${USER_STATUS.ACTIVE}传递。

status

答案 1 :(得分:0)

我尝试了一下,它起作用了:

  const users = await User.findAll({
    attributes: [
      'id',
      'firstName',
      'lastName',
      Sequelize.literal(`"firstName" || ' ' || "lastName" as name`)
    ],
    where: { status: { [Op.contains]: [USER_STATUS.ACTIVE] }, organizationId: orgId },
    include: [{ model: Role, as: 'roles', where: { roleName: role } }]
  }).map(u => {
    return u.dataValues;
  });