问题
Here's a sample error message generated by Loopback for unique item:
{
"error": {
"statusCode": "422",
"name": "Error",
"message": "ER_DUP_ENTRY: Duplicate entry 's@a.com' for key 'email'",
"code": "ER_DUP_ENTRY"
}
}
However, I want to modify it as:
{
"errors": {
"email": [
"The field email should be unique"
]
}
}
Documentation really didn't help me. Can anybody help me on this please?
回答1:
You can call the next method with the custom error object.
let error = new Error('Custom Error message.'); //Message passed as parameter.
error.name = "UNIQUE_EMAIL";
error.status = 422; //Set status code
next(error); // call the next method with error object.
LoopBack requires the error object to have a message, status, and the name property.
Refer to there documentation: https://loopback.io/doc/en/lb3/Error-object.html
来源:https://stackoverflow.com/questions/54832216/how-to-modify-error-messages-in-loopback-4