Answer the question
In order to leave comments, you need to log in
Why is UserInputError not displaying properties?
Hello.
When registering, I validate the fields, and I want to display an error that will contain a list of informers for each empty field:
import validator from "validator";
export default (name: string, email: string, password: string) => {
const errors: any = {};
if (!name || name.trim() === "") {
errors.name = "Укажите пожалуйста имя";
}
if (!email || email.trim() === "") {
errors.email = "Укажите пожалуйста email";
} else {
if (!validator.isEmail(email.trim())) {
errors.email = "Укажите пожалуйста корректный email";
}
}
if (!password || password === "") {
errors.password = "Укажите пожалуйста пароль";
}
return {
errors,
valid: Object.keys(errors).length < 1
}
};
import {UserInputError} from "apollo-server-express";
const {valid, errors} = registrationInputValidator(name, email, password);
if (!valid) {
throw new UserInputError("Ошибка", {errors});
}
{
"errors": [
{
"message": "Ошибка",
"locations": [
{
"line": 2,
"column": 3
}
],
"path": [
"registration"
],
"extensions": {
"code": "BAD_USER_INPUT"
}
}
],
"data": {
"registration": null
}
}
console.log(new UserInputError("Ошибка", {errors}));
UserInputError: Ошибка
{
errors: {
name: 'Укажите пожалуйста имя',
email: 'Укажите пожалуйста email',
password: 'Укажите пожалуйста пароль'
},
extensions: {code: 'BAD_USER_INPUT'}
}
Answer the question
In order to leave comments, you need to log in
I solved the problem, instead of graphqlhttp I use new ApolloServer
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question