Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/symfony/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Feathersjs 使用FeatherJS和Joi进行唯一的字段验证_Feathersjs_Joi_Nedb - Fatal编程技术网

Feathersjs 使用FeatherJS和Joi进行唯一的字段验证

Feathersjs 使用FeatherJS和Joi进行唯一的字段验证,feathersjs,joi,nedb,Feathersjs,Joi,Nedb,使用FeatherJS,我定义了如下用户模型: 从“NeDB”进口NeDB; 从“路径”导入路径; 从“../declarations”导入{Application}; 导出默认功能应用程序:应用程序:NeDB{ constdbpath=app.get'nedb'; 常数模型=新NeDB{ 文件名:path.joindbPath,“users.db”, 自动加载:对, }; Model.ensureIndex{ 字段名:“电子邮件”, 独一无二:没错, }; 收益模型; } 由于用户挂钩上的Jo

使用FeatherJS,我定义了如下用户模型:

从“NeDB”进口NeDB; 从“路径”导入路径; 从“../declarations”导入{Application}; 导出默认功能应用程序:应用程序:NeDB{ constdbpath=app.get'nedb'; 常数模型=新NeDB{ 文件名:path.joindbPath,“users.db”, 自动加载:对, }; Model.ensureIndex{ 字段名:“电子邮件”, 独一无二:没错, }; 收益模型; } 由于用户挂钩上的Joi,我确保了一些验证:

从“@featherjs/authentication”导入*作为FeatherAuthentication; 从“@featherjs/authentication local”以本地方式导入*; 从“公共”导入{disallow}; 从“@hapi/Joi”导入Joi; 从“../../hooks”导入{validate}; const{authenticate}=featherAuthentication.hooks; const{hashPassword,protect}=local.hooks; 常量验证=验证 Joi.object{ 电子邮件:Joi.string.email.alter{ create:schema=>schema.required, }, 密码:Joi.string.min8.alter{ create:schema=>schema.required, }, }, ; 导出默认值{ 之前:{ 全部:[], 查找:[ 验证“jwt”, ], 获取:[ 验证“jwt”, ], 创建:[ 验证, hashPassword'password', ], 更新:不允许, 补丁:[ hashPassword'password', 验证“jwt”, ], 删除:[ 验证“jwt”, ], }, // ... }; Joi验证正在工作,如果提交的数据无效,则会产生错误的请求错误

独特的索引阻止用户使用现有的电子邮件地址创建帐户,但它会产生一个基本的500错误实例,很难在我的前台应用程序上利用

有没有一种方法可以处理Joi的唯一验证,或者我应该重新思考我的逻辑


注意:我在这里使用的是NeDB,但移动到Mongo也是可能的。

我也遇到了这个问题。我解决这个问题的方法是从钩子访问服务,检查是否已经存在具有相同电子邮件的帐户

在你的钩子里,添加类似的东西

const userService = context.app.service(“users”);// get the users service
const result = await userService.find({query: {email: context.data.email }});
if (result.total > 0) { // throw an error because an account already exists.}
这个解决方案适合我,因为它与任何类型的数据库或包都是解耦的

我们在这里所做的是询问用户服务是否已经存在使用该服务的findmethod发送相同电子邮件的用户。如果该服务返回一条记录,即result.total>0,则该服务已经有一个用户使用相同的电子邮件。因此,我们抛出一个异常


您可以了解有关查询服务的更多信息

谢谢。我确实在考虑类似的事情,但我想知道是否可以使用较少手动的方法。@Soullivaneuh老实说,较少手动的解决方案将涉及某种特定堆栈的耦合,因为您需要能够查询用户数据库以检查唯一性。另一种选择是在Mongoose模型中执行类似email:{unique:true}的操作,在数据库级别进行检查。缺点是数据层的耦合验证逻辑。但这是一个选择。谢谢!我说的不是实话。事实上,我对NodeJS env还很陌生,所以我正在学习-