PostgreSQL,如果行不存在,则Sequelize返回null

时间:2020-03-31 16:16:14

标签: node.js postgresql sequelize.js

当我调用数据库并且没有所需的行时,查询返回null,我可以使查询返回所有现有字段吗

supplierRouter.get('/:supplier_id', async (req, res) => {
  let [supplier] = await Promise.all([
    models.supplier.model.findOne({
      attributes: [
        'id',
        'name',
        'order',
        'type',
        'credit_card_id',
        'max_payment',
        'max_percentage_profit',
        'min_percentage_profit',
        'status',
        Sequelize.literal(`
              "SupplierContact"."name" AS "contact_name",
              "SupplierContact"."phone" AS "contact_phone_number",
              "SupplierColor"."logo_background" AS "logo_background_color",
              "SupplierColor"."light_logo_background_color" AS "light_logo_background",
              "Address"."street" AS "address",
              "Document"."url" AS "image", 
              "LL_Document"."url" AS "light_logo"
           `),
      ],
      include: [
        { model: models.supplierContact.model, as: 'SupplierContact' },
        { model: models.address.model, as: 'Address' },
        { model: models.supplierColor.model, as: 'SupplierColor' },
        {
          model: models.business.model,
          attributes: ['id'],
          through: {
            attributes: [['supplier_id', 'id']],
            where: { is_authorized: true },
          },
          as: 'Business',
          require: true,
        },
        {
          model: models.document.model,
          as: 'Document',
          where: { type: getTypeNum('image', 'Document') },
          through: {
            attributes: [['supplier_id', 'id']],
          },
          require: false,
        },
        {
          model: models.document.model,
          require: false,
          as: 'LL_Document',
          where: { type: getTypeNum('light_logo', 'Document') },
          through: {
            attributes: [['supplier_id', 'id']],
          },
        },
      ],
      raw: true,
      where: {
        id: req.params.supplier_id,
        '$Business.id$': { [Op.ne]: null },
      },
      includeIgnoreAttributes: false,
    }),
  ]).catch((e) => console.error(e));
  console.log(supplier);
  res.send(supplier);
});

在这种情况下,我没有“ light_logo”字段。而且我需要从DB中获取一个没有此字段的对象(如果不存在)。我该怎么办?

0 个答案:

没有答案