获取 2 条记录而不是仅 1 条记录

问题描述 投票:0回答:1

目前我有一个奇怪的行为,这是我在数据库上的数据。问题是数据库中的

''
-> 空字符串,但
NULL
值仍然有效。

# id, user_type, mobile_number, email
'65', '2', '+639123456782', 'sample@mail'
'70', '1', '+639123456781', ''

关于邮递员

{
    "email": "sample@mail"
    // "mobile_number": "+639123456781"
}

这应该只返回 1 条记录

只有当它为NULL时才起作用并返回1条记录

# id, user_type, mobile_number, email
'65', '2', '+639123456782', 'sample@mail'
'70', '1', '+639123456781', NULL

这是我的代码

let user;
// If both email and mobile_number are provided, find users that match either criteria.
if (email && mobile_number) {
  // Find users with the provided email or mobile number
  user = await User.findAll({
    where: {
      [Op.or]: [{
          email: email
        },
        {
          mobile_number: mobile_number
        }
      ],
      delete_flg: false,
    },
  });
} else if (email) {
  // If only email is provided, find users with the provided email.
  // Adjusted to treat null and empty strings as equivalent
  user = await User.findAll({
    where: {
      [Op.or]: [{
          email: email
        },
        Sequelize.where(Sequelize.fn('COALESCE', Sequelize.col('email'), null), email)
      ],
      delete_flg: false,
    },
  });
} else if (mobile_number) {
  // If only mobile_number is provided, find users with the provided mobile number.
  // Adjusted to treat null and empty strings as equivalent
  user = await User.findAll({
    where: {
      [Op.or]: [{
          mobile_number: mobile_number
        },
        Sequelize.where(Sequelize.fn('COALESCE', Sequelize.col('mobile_number'), null), mobile_number)
      ],
      delete_flg: false,
    },
  });
}

// If we have users from the initial search, we need to ensure we're considering both email and mobile number.
if (user && user.length > 0) {
  // Extract emails and mobile numbers from the found users
  const emails = user.map(u => u.email);
  const mobileNumbers = user.map(u => u.mobile_number);

  // Find users that match the emails or mobile numbers extracted from the initial search
  user = await User.findAll({
    where: {
      [Op.or]: [{
          email: {
            [Op.in]: emails
          }
        },
        {
          mobile_number: {
            [Op.in]: mobileNumbers
          }
        }
      ],
      delete_flg: false,
    },
  });
}
mysql node.js sequelize.js
1个回答
0
投票

查看您的问题,发现您的代码只是检查电子邮件字段包含数据,因为电子邮件字段中的值为 EMPTY(如“”)意味着它包含与 NULL 不同的空值,因此当您将该字段中的值设置为 NULL 时那么您只会收到一条记录。简而言之,您可以针对您的电子邮件地址添加一些小的验证代码即可解决该问题。这里我给大家补充一下。

// Validate email format
function isValidEmail(email) {
    // Regular expression for basic email validation
    const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
    return emailRegex.test(email);
}

// Find users with the provided email or mobile number
user = await User.findAll({
    where: {
        [Op.or]: [
            {
                email: email && isValidEmail(email) ? email : null
            },
            {
                mobile_number: mobile_number
            }
        ],
        delete_flg: false,
    },
});

请告诉我问题是否解决了您的问题。

© www.soinside.com 2019 - 2024. All rights reserved.