smaver smaver - 1 year ago 64
SQL Question

Assign the value of a function to a variable

I am trying to assign the result of a MySQL query to a variable so I can send it to a post request. Here is my code:

// mysqlCRUD.js

buscarUsuario: function(usuario, cb) {
con.query('SELECT * FROM usuarios WHERE nick = ?', usuario, function(err, results) {
return cb(null, results.length);


//index.js'/comprobarUsuario', function(req, res, next) {
resultado = mysqlCRUD.buscarUsuario(req.body.nick, function(err, data){
return data;

The post request comes from a jquery:

$.post('/comprobarUsuario', { nick: nick}, function(data) {
console.log("Respuesta Usuario: " + data);

But I dont know why the result never reaches the post request.

Could anybody please help me.


Answer Source

From the documentation

The body parameter can be a Buffer object, a String, an object, or an Array.'/comprobarUsuario', function(req, res, next) {
    mysqlCRUD.buscarUsuario(req.body.nick, function(err, data){
       return res.send(data.toString());