Validation message alway return null

#1

Hello Guy
I got stuck on Validation message is seem alwaysreturn null

async register({ request, response }) {
   const rules = {
     email: "required|email|unique:users,email",
     username: "required|unique:users,username",
     password: "required"
   };
   console.log(rules);
   const { email, username, password } = request.only([
     "email",
     "username",
     "password"
   ]);

   const validation = await validate({ email, username, password }, rules);
   // return validation;
   if (!validation.fails()) {
     try {
       const user = await User.create({ email, username, password });
       console.log("1");
       return response.send({ message: "User has been created" });
     } catch (err) {
       console.log("2");
       response.status(401).send({ error: "Please try again" });
     }
   } else {
     response.status(401).send(validation.messages());
   }
 }

0 Likes

#2
  • Is the connection to the bank correct?
  • Is there a ‘username’ column?

I usually do the method a little differently.

async register({ request, response }) {

        const rules = {
          email: "required|email|unique:users,email",
          username: "required|unique:users,username",
          password: "required"
        };
        
        const { email, username, password } = request.all();
     
        const validation = await Validator.validate({ email, username, password }, rules);
        
        if (validation.fails()) {
            return response.status(422).send(validation.messages());
        }

        try {
            const user = await User.create({ email, username, password });
            console.log("1");
            return response.status(200).send({ message: "User has been created" });
        } catch (err) {
            console.log("2");
            return response.status(500).send({ error: "Please try again", exception: err.message });
        }
        
    }
1 Like

#3

You can always use adonis persona which will save you ton of time :slight_smile:

0 Likes

#4

Can you console log validation.messages() to see what it prints

0 Likes

#5

I discovered @krissnawat

In 2 response the RETURN is missing.
Look at the post I just made, I put her fix …

0 Likes

#6

blank

0 Likes

#7

blank

0 Likes