【问题标题】:Search for user by email or by primary key of type uuid通过电子邮件或 uuid 类型的主键搜索用户
【发布时间】:2021-06-27 05:12:08
【问题描述】:

我将 typeorm 与 typescript 和 postgresql 驱动程序一起使用

我的控制器中有这段代码:

const userRepository = getCustomRepository(UserRepositories);
const query = { by_email: {where: {email: user_receiver} }, by_uuid: user_receiver }
const isValidReceiver = user_receiver.includes("@") ? await userRepository.findOne(query.by_email) : await userRepository.findOne(query.by_uuid)

有什么方法可以简化这个查询或检查用户接收者是电子邮件还是 uuid?

【问题讨论】:

    标签: node.js typescript typeorm


    【解决方案1】:

    您使用以下函数来检查是否是有效的电子邮件,yup.js 或其他一些库为您检查它

    const isEmail = (email: string) => {
          const regex = /^(([^<>()[\]\\.,;:\s@"]+(\.[^<>()[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
        
          if (!regex.test(email)) {
            return false
          }
          
          return true
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-28
      • 2015-01-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      相关资源
      最近更新 更多