E
E
Egor Astreiko2019-08-01 10:55:15
Express.js
Egor Astreiko, 2019-08-01 10:55:15

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:

Validation

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
    }
};


Validator call

import {UserInputError} from "apollo-server-express";
const {valid, errors} = registrationInputValidator(name, email, password);
if (!valid) {
    throw new UserInputError("Ошибка", {errors});
}


When performing a mutation, I get a response

{
  "errors": [
    {
      "message": "Ошибка",
      "locations": [
        {
          "line": 2,
          "column": 3
        }
      ],
      "path": [
        "registration"
      ],
      "extensions": {
        "code": "BAD_USER_INPUT"
      }
    }
  ],
  "data": {
    "registration": null
  }
}


Why is properties not displayed in the response?
Although
console.log(new UserInputError("Ошибка", {errors}));

in the console displays:
UserInputError: Ошибка
            {
                errors: {
                    name: 'Укажите пожалуйста имя',
                    email: 'Укажите пожалуйста email',
                    password: 'Укажите пожалуйста пароль'
                },
                extensions: {code: 'BAD_USER_INPUT'}
            }

Or how else can you display an object with fields and what are the errors in them?
thanks in advance.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Egor Astreiko, 2019-08-02
@Egor1324

I solved the problem, instead of graphqlhttp I use new ApolloServer

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question