似乎错误消息包含在文本中。假设在模型验证中,如果记录已存在,我只想将“存在”发送给客户端。
一个服务器也许我会做类似的事情:
validate: {
isEmail: true,
isUnique: function (email, done) {
console.log("checking to see if %s exists", email);
user.findOne({ where: { email: email }})
.then(function (user) {
done(new Error("exists"));
},function(err) {
console.error(err);
done(new Error('ERROR: see server log for details'));
}
);
}
}
在客户端,我可能会这样做:
feathers.service('users').create({
email: email,
password: password
})
.then(function() {
console.log("created");
})
.catch(function(error){
console.error('Error Creating User!');
console.log(error);
});
打印到控制台的错误是:
"Error: Validation error: exists"
如何只发送“存在”一词而不发送额外的文本?我真的想发回一个自定义对象,但我似乎找不到任何这样做的例子。我见过的最接近的是:https://docs.feathersjs.com/middleware/error-handling.html#featherserror-api
但我还没有弄清楚如何在验证器中使这样的事情起作用。
最佳答案
Feathers 不会更改任何错误消息,因此 Validation error:
前缀可能是由 Mongoose 添加的。
如果您想更改消息或发送全新的错误对象,从 Feathers-hooks v1.6.0 开始,您可以使用错误 Hook :
const errors = require('feathers-errors');
app.service('myservice').hooks({
error(hook) {
const { error } = hook;
if(error.message.indexOf('Validation error:') !== -1) {
hook.error = new errors.BadRequest('Something is wrong');
}
}
});
您可以阅读有关错误和应用程序 Hook 的更多信息 here
关于javascript - FeathersJS:如何将非主观错误传递回客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42079436/