Trigger does not run

0

I am using the express framework to learn how to create a simple resample with node and mysql and I have the following code:

router.post('/CrearUsuario',function(req,res,next){
    postData = Array()
    postData.push(req.body.primerNombre);
    postData.push(req.body.segundoNombre);
    postData.push(req.body.primerApellido);
    postData.push(req.body.segundoApellido);
    postData.push(req.body.usuario);
    postData.push(req.body.contrasena);
    postData.push(req.body.administrador);
    console.log(postData);
    conection.query('insert into usuarios (primerNombre,segundoNombre,primerApellido,segundoApellido,usuario,contrasena,administrador) values (?)',[postData] ,function(error,result,fields){
    if(error) throw error
        res.json({ code : 200 , data : "Usuario ha sido agregado con exito"});
    })
})

I sent the following json from the insomnia software to that address:

{
"primerNombre":"Marcia",
"segundoNombre":"Cristina",
"primerApellido":"Aguilera",
"segundoApellido":null,
"usuario":"marciAguilera",
"contrasena":"marcia1234",
"administrador":false
}

In the database I have the following trigger:

DELIMITER |
CREATE TRIGGER encodePass BEFORE INSERT ON usuarios
FOR EACH ROW 
BEGIN
SET NEW.contrasena = AES_ENCRYPT(NEW.contrasena,@pass);
END |
DELIMITER ;

Now if I do an insert from the database console this trigger erases the password, but if I do it from the api that executes an insert in the database, the password appears as null.

Example of data inserts in the bd those with the encrypted password were entered directly from the console and those with null were entered from the api, why is this happening?

How can I fix it so that it encrypts when data is entered from the api?

    
asked by Cristian Aragon 26.07.2018 в 23:36
source

1 answer

0

The problem was that in the session that was done with node the @pass variable did not exist therefore the function of encrypting failed returning a null, the solution is to create @pass in the trigger

    
answered by 27.07.2018 в 18:50