type here
Im using node.js to connect an react native application to a mysql database.
I created a procedure that checks if the user is loggin for the first time. If is loggin for the first time will return 2, if user loggin for the firs time but the password is incorrect will return 3.
If is not loggin for the first time and the password is correct will return a json with a table data
If is not loggin for the first time and the password is incorrect will return 3.
CREATE DEFINER=`root`@`localhost` PROCEDURE `loginColaborador`(
IN email varchar(100),
IN senhaColaborador varchar(255),
out resposta varchar(16383)
)
BEGIN
declare padrao varchar(50);
declare atual varchar(50);
set padrao = (select senhaColaboradorPadrao from tbcolaborador where emailColaborador = email);
set atual = (select senhaColaboradorAtual from tbcolaborador where emailColaborador = email);
SET resposta = '';
if padrao is null then
if(atual = senhaColaborador)then
SELECT CONCAT('[', GROUP_CONCAT(JSON_OBJECT('Id', idquestionario, 'Questionario', nomeQuestionario)), ']')
INTO resposta
FROM tbquestionario q
WHERE q.idempresa = (SELECT idempresa FROM tbcolaborador WHERE emailColaborador = email);
else
set resposta = '1';
end if;
else
if (select senhaColaboradorPadrao from tbcolaborador where emailColaborador = email) = senhaColaborador then
set resposta = '2';
else
set resposta = '3';
end if;
end if;
END
when im testing using mysql commands at workbench it works perfectly and i get my answers correct
CALL loginColaborador("[email protected]", "abcd", @resposta);
SELECT @resposta as resposta;
but when i try using my requisition at postman, using the application returns an mysql syntax errorr
requisition node:
app.post('/api/loginColaborador', (req, res) => {
const { email, senhaColaborador } = req.body;
const query = 'CALL loginColaborador(?, ?, @resposta); (SELECT @resposta as resposta);';
pool.query(query, [email, senhaColaborador], (error, results) => {
if (error) {
console.error(error);
return res.status(500).json({ message: 'Erro ao fazer login' });
}
// Pegar o valor da variável de saída `resposta`
const resposta = results[1][0].resposta;
if (resposta === '1') {
return res.status(401).json({ message: 'Senha atual incorreta' });
} else if (resposta === '2') {
return res.status(200).json({ message: 'Senha padrão correta' });
} else if (resposta === '3') {
return res.status(401).json({ message: 'Senha padrão incorreta' });
} else {
return res.status(200).json({ questionarios: JSON.parse(resposta) });
}
});
});
error:
Error: ER_PARSE_ERROR: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘(SELECT @resposta as resposta)’ at line 1
at Sequence._packetToError (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibprotocolsequencesSequence.js:47:14)
at Query.ErrorPacket (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibprotocolsequencesQuery.js:79:18)
at Protocol._parsePacket (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibprotocolProtocol.js:291:23)
at Parser._parsePacket (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibprotocolParser.js:433:10)
at Parser.write (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibprotocolParser.js:43:10)
at Protocol.write (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibprotocolProtocol.js:38:16)
at Socket. (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibConnection.js:88:28)
at Socket. (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibConnection.js:526:10)
at Socket.emit (node:events:518:28)
at addChunk (node:internal/streams/readable:559:12)
——————–
at Pool.query (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesmysqllibPool.js:199:23)
at C:UsersenzzoOneDriveDesktopnodemysqlindex.js:69:10
at Layer.handle [as handle_request] (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesexpresslibrouterlayer.js:95:5)
at next (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesexpresslibrouterroute.js:149:13)
at Route.dispatch (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesexpresslibrouterroute.js:119:3)
at Layer.handle [as handle_request] (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesexpresslibrouterlayer.js:95:5)
at C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesexpresslibrouterindex.js:284:15
at Function.process_params (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesexpresslibrouterindex.js:346:12)
at next (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulesexpresslibrouterindex.js:280:10)
at cors (C:UsersenzzoOneDriveDesktopnodemysqlnode_modulescorslibindex.js:188:7) {
code: ‘ER_PARSE_ERROR’,
errno: 1064,
sqlMessage: “You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘(SELECT @resposta as resposta)’ at line 1”,
sqlState: ‘42000’,
index: 0,
sql: “CALL loginColaborador(‘[email protected]’, ‘abcd’, @resposta); (SELECT @resposta as resposta);”
}
enzz5 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.