smaver smaver - 6 months ago 23
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) {
console.log(usuario);
con.query('SELECT * FROM usuarios WHERE nick = ?', usuario, function(err, results) {
return cb(null, results.length);
});

},

//index.js


router.post('/comprobarUsuario', function(req, res, next) {
resultado = mysqlCRUD.buscarUsuario(req.body.nick, function(err, data){
return data;
});
res.send(resultado);
});


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.

THANKS.

Answer

From the documentation http://expressjs.com/en/4x/api.html

The body parameter can be a Buffer object, a String, an object, or an Array.

router.post('/comprobarUsuario', function(req, res, next) {
    mysqlCRUD.buscarUsuario(req.body.nick, function(err, data){
       return res.send(data.toString());
    });
});
Comments