【问题标题】:How to check if email exsit in database with yup如何使用yup检查数据库中是否存在电子邮件
【发布时间】:2022-11-03 11:43:55
【问题描述】:

我想检查数据库中是否存在电子邮件。我写了这样的东西,但我不知道如何从 axios 传递结果。

email: yup
  .string()
  .email('type correct email')
  .required('email is required')
  .test(
    'email check',
    'email exists in the database', 
    async (value) => {
    await AuthServices.checkEmailExist(value).then((response) => {  
    console.log(response.data)
    })
  }

如果电子邮件存在,我的 response.data 返回 true,如果电子邮件不在数据库中,则返回 false。如何判断响应是真还是假?

【问题讨论】:

    标签: reactjs validation axios response yup


    【解决方案1】:

    所有测试都必须提供名称、错误消息和验证函数,当当前值有效时必须返回 true,否则返回 false 或 ValidationError。要使测试异步,请返回解析 true、false 或 ValidationError 的 Promise。

    因此,您可以尝试:

    email: yup
      .string()
      .email('type correct email')
      .required('email is required')
      .test("email check", "email exists in the database", async value => {
            (await AuthServices.checkEmailExist(value).data) === "true";
        })
    

    【讨论】:

      猜你喜欢
      • 2021-10-10
      • 2012-03-13
      • 2019-07-13
      • 1970-01-01
      • 2014-03-15
      • 1970-01-01
      • 2014-05-10
      • 2021-04-24
      相关资源
      最近更新 更多