TypeError: Cannot read property '0' of null

Hi people! I have a big problem, recently i added the “validation provider” to adonis but when i try save a one entry in data base adonis give me this error : TypeError: Cannot read property ‘0’ of null …
this is my code :

const User = use('App/Models/User')
const Hash = use('Hash')
const Post = use('App/Models/Post')
const Cloudinary = use('Cloudinary')
const { validate } = use('Validator')

class UserController {

    ///Sign up

    async signup ({ request, auth, response }) {
		// get user data from signup form
		const userData = request.only(['name', 'username', 'email', 'password']);
		//console.log(userData);
		try {
            
            const rules = {
                name: 'required|string|max:10|alpha',
                username: 'required|string|max:10|unique:users,username',
                email: 'required|string|max:50|min:10|unique:users,email',
                password: 'string|required|min:8|max:20',
            }
            const messages = {
                required: 'Es necesario llenar todos los campos',
                'name.alpha':'Nombre no puede contener simbolos ni espacios',
                'username.max' : 'Nombre de usuario debe ser menor a 10 caracteres',
                'username.unique' : 'Este nombre de usuario ya está ocupado',
                'email.unique': 'Este correo ya está registrado',
                'email.min':'Correo no puede ser inferior a 10 caracteres',
                'email.max' : 'Correo no puede ser mayor a 50 caracteres',
                'name.max' : 'Nombre no debe tener mas de 10 caracteres',
                'password.min' : 'Contraseña debe tener al menos 8 caracteres',
                'password.max' : 'Contraseña no puede ser mayor a 20 caracteres'
              }

            const validation = await validate(userData, rules, messages)
            if (validation.fails){
                const message = validation.messages()
                let error = message[0]
                return response.status(400).json({
                    status: 'wrong',
                    message: error.message
                })
            } else {
                // save user to database
                const user = await new User()
                user.name = userData.name
                user.username = userData.username.replace(/ /g, "_")
                user.email = userData.email
                user.password = userData.password
                await user.save();
                // generate JWT token for user
                console.log("Creating token");
                const token = await auth.generate(user)
                console.log("Success");
                

                return response.json({
                    status: 'success',
                    data: token
                })

            }
		} catch (error) {
			console.log(error);
			return response.status(400).json({
				status: 'error',
				message: 'Error interno, intentalo mas tarde'
			})
		}
	}

my packgage.json :

{
  "name": "adonis-api-app",
  "version": "4.1.0",
  "adonis-version": "4.1.0",
  "description": "Adonisjs boilerplate for API server with pre-configured JWT",
  "main": "index.js",
  "scripts": {
    "start": "node server.js",
    "test": "node ace test"
  },
  "keywords": [
    "adonisjs",
    "adonis-app"
  ],
  "author": "",
  "license": "UNLICENSED",
  "private": true,
  "dependencies": {
    "@adonisjs/ace": "^5.0.8",
    "@adonisjs/auth": "^3.0.7",
    "@adonisjs/bodyparser": "^2.0.5",
    "@adonisjs/cors": "^1.0.7",
    "@adonisjs/fold": "^4.0.9",
    "@adonisjs/framework": "^5.0.9",
    "@adonisjs/ignitor": "^2.0.8",
    "@adonisjs/lucid": "^6.1.3",
    "@adonisjs/validator": "^5.0.6",
    "@adonisjs/websocket": "^1.0.12",
    "adonis-4-cloudinary": "^1.0.2",
    "adonis-notifications": "^1.1.2",
    "adonis-validation-provider": "^3.0.5",
    "mariadb": "^2.0.5",
    "mysql": "^2.17.1"
  },
  "devDependencies": {},
  "autoload": {
    "App": "./app"
  }
}

thx 4 the help

1 Like

Hi @Scute300 :slight_smile:

Can you change

validation.fails

with (docs)

validation.fails()

to be sure it’s not the problem

1 Like