javascript - 来自 Yup 的自定义验证的错误消息不会消失
问题描述
作为注册过程的一部分,我想使用来自以下的自定义验证来检查任何重复的电子邮件Yup
:
validationSchema={yup.object().shape({
email: yup
.string()
.email()
.test({
name: 'duplicate-email-check',
params: 'value',
message: 'Duplicate email already exists',
test: async (value) => {
firebase
.auth()
.fetchSignInMethodsForEmail(value)
.then(result => {
if (result === "password") {
return false
} else {
return true
}
})
.catch(err => console.log(err))
}
})
.required(),
})}
我正在使用fetchSignInMethodsForEmail来获取具有相同电子邮件的任何类型的帐户,如果存在,则会抛出验证错误消息。我在混合()。文本()模式之后建模,但问题是错误消息“重复的电子邮件已经存在”即使没有重复的电子邮件出现,它也不会消失。
解决方案
这部分代码不返回返回布尔值的 Promisetest
test: async (value) => { // Notice this, adding curly braces will require you to put a return statement
firebase
.auth()
.fetchSignInMethodsForEmail(value)
.then(result => {
if (result === "password") {
return false
} else {
return true
}
})
.catch(err => console.log(err))
}
您可以将代码更改为:
test: async (value) => { // Notice this, adding curly braces will require you to put a return statement
return firebase // Notice I added the return statement
.auth()
.fetchSignInMethodsForEmail(value)
.then(result => {
if (result === "password") {
return false
} else {
return true
}
})
.catch(err => console.log(err))
}
推荐阅读
- angular - 基于条件如何设置指令的布尔值 - Angular
- python - 使用 python 将 XES 转换为 CSV
- c++ - GCC 的 -Wsuggest-override 或 Clang 的 -Winconsistent-missing-override 的 MSVC 等价物是什么?
- apache-spark - 为什么 lit() 函数在 selectExpr() 中不起作用?
- java - 如何将值从 xml 映射到 XSLT
- javascript - 如何打印按道具值过滤的对象的道具名称
- shopware - 在 Shopware 6 中的产品详细信息页面上显示所有变体
- discord.py - 如何使命令前缀不区分大小写
- css - 为什么 CSS 网格会在网格线结束后多留一个空隙?
- c# - 在 EF Core 3.1 中替换 MapToStoredProcedures() 的最佳方法是什么?