1
module.exports.getTragos = function(req, res) {
    var connection = conectar();
    connection.connect();

    connection.query('SELECT * from tipoAlcohol', function(err, rows, fields) {
        if (!err)
            return JSON.stringify(rows));
        else
            return  'Error while performing Query.';
    });

    connection.end();
}

I would like to make the getTragos function to return the rows of the query executed, but return JSON.stringify(rows)); is not returning them. I tried to put console.log(rows) and the query is executed fine.

This is the code that invokes getTragos:

module.exports.iniciar = function(app) {
    app.get('/bebidas/getTipoAlcohol', function (req, res) {
        res.send(modelo.getTragos());
    });
}

3 Answers 3

2

Just write the result in response, like this.

module.exports.iniciar = function(app) {
    app.get('/bebidas/getTipoAlcohol', function(req, res, next) {
      modelo.getTragos(function(err, tragos) {
        if(err) {return next(err);}
        //do you processing here
        // then write the result to the response
        var result = doSomething(tragos); // i am assuming that doSomething is synchronous. 
        res.json(result);
      });
    });
}

module.exports.getTragos = function(callback) {
    var connection = conectar();
    connection.connect();

    connection.query('SELECT * from tipoAlcohol', function(err, rows, fields) {
        connection.end();
        if (!err)
            return callback(null, rows);
        else
            return callback('Error while performing Query.');
    });

}

Notice, I changed the signature of both the methods.

Sign up to request clarification or add additional context in comments.

7 Comments

hey xyz, it worked! i have a question, what if i want to do something with the results of the query before sending them to the client? for example: module.exports.iniciar = function(app){ app.get('/bebidas/getTipoAlcohol', function (req, res) { var response = modelo.getTragos(); //do some procesing to response res.send(response); }); } i tried it like that and i cannot get the response to work
In that case, you have to make some changes in the getTragos function signature and make it to accept a callback. Check my answer(not right away, i am just going to change :-) )
thanks!! im getting a response, but they are no longer the rows of the resultset, im getting [Object Object] , [Object Object] , [Object Object] as a response, how do i fix this?
could you explain me how to get the callback outside the scope in here? module.exports.iniciar = function(app) { app.get('/bebidas/getTipoAlcohol', function(req, res, next) { var tragos; modelo.getTragos(function(err, tragos) { if(err) {return next(err);} //do you processing here // then write the result to the response // var result = doSomething(tragos); // i am assuming that doSomething is synchronous. res.json(tragos); }); console.log(tragos); }); }
the idea behind this is that if i need to perform a query that depends on the results of another query i can have them stored on a variable like: var tragos = modelo.getTragos(...); var tragosArray=[]; for (i=0;i<tragos.length;i++) { tragosArray.push(modelo.getBebida(tragos[i])); }
|
2
module.exports.getTragos = function(req, res, callback) {
    var connection = conectar();
    connection.connect();

    connection.query('SELECT * from tipoAlcohol', function(err, rows, fields) {
        if (!err)
            callback(rows);               
        else
            return  'Error while performing Query.';
    });

    connection.end();
}


module.exports.iniciar = function(app) {
    app.get('/bebidas/getTipoAlcohol', function (req, res) {
        modelo.getTragos(req, res, function(returnedValue){
            res.send(returnedValue);
        });        
    });
}

6 Comments

im getting an error, it says: "Callback is not a function"
Call you function like this: modelo.getTragos(req, res, function(returnedValue)){....}
great! thanks viddesh !! do you know how can i get the resulset into a variable? i mean this part: modelo.getTragos(req, res, function(returnedValue){ res.send(returnedValue); }); how can i do to have something like var resulset = (whats inside returnedvalue)
You don't need to do that. You can use returnedValue itself for any operations using the column name and row index. Row index starts from 0. so returnedValue[0] will have whole first row of the query result. Now further you access values lets say there are 2 columns viz., name and age. If you want to value of name in first row you can get it from: returnedValue[0].name.
but i want to have the value outside the function, i mean: now i can access returnvalue inside this: function(returnedValue){ res.send(returnedValue); }); i would like to have it in an upper level, above modelo.getTragos(req,res
|
1

Don't use JSON.stringify(), just return rows.

6 Comments

im still not getting anything returned
What response do you get? Can you print the value of rows?
if i change return rows; for console.log(rows) then i see the result of the query on the console, but i want to have the result of the query returned
You can do this using callback, remove the return statement. If you have no idea I can give the code.
Look at my second answer.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.