0

I have this function to connect to my DB and consult something in it.

function conn (text){  
var mysql      = require("mysql");
var connection = mysql.createConnection({
    connectionLimit : 100, //important
    host     : 'xxx.xxx.xxx.xxx',
    user     : 'xxxxxx',
    password : 'xxxxxxx',
    database : 'xxxxxxxx'
});

connection.connect();

var queryString = 'SELECT usuario.idUsuario FROM usuario WHERE usuario.nickname = ' + '"' +  text + '"';  

function getID(text, callback){
connection.query(queryString, function(err, rows, fields) {
  var id;
    if (err){ 
      callback(err, null);
    }else{
      for (var i in rows) {
          id = rows[i].idUsuario;
      }
      callback(null, id);
    }
});
}

var result;
    getID(text, function(err, content) {
    if (err) {
      console.log(err);
    } else {
      result = content;
    }
});
connection.end();
};

Now, i need to get the result in other variable to use it in other functions inside my JS file. How can i get that value without get code inside the variable?

4
  • 2
    You can't do that. You need to use callbacks or promises everywhere. blog.slaks.net/2015-01-04/async-method-patterns Commented Jul 25, 2016 at 18:23
  • im using calbacks in the function getID, but maybe im doing something wrong with this callbacks Commented Jul 25, 2016 at 18:25
  • You need to return the value from your function using a callback too. Commented Jul 25, 2016 at 18:41
  • There is a problem with it, im started with JS just a few days ago and there's a lot of things that I dont understand. Im traying to make a bot to work in "slack" platform, and i need to use NodeJS and Howdy.ai Botkit. Can you explain me how to get the result? Commented Jul 25, 2016 at 19:08

1 Answer 1

1

Your function conn (text) is fetching data using callbacks, you need to carry that pattern all the way to the consuming code. I'm not sure what the "other functions" look like, but let's pretend one looks like this:

function awesomeFunction() {
  var sweetResult = conn('sweet nickname');
  alert(sweetResult);
}

conn is going to connect and issue a query to your db. You do not want to stop everything else and wait for the result to come back, you want the db library to call you when it has results, hence the callbacks. Since the database isn't stopping everything, control returns to your conn function, and then back to awesomeFunction, and passes to the next line alert(sweetResult). But wait! You didn't return anything from conn, but more important, the database hasn't even called back with results yet!

So, you need awesomeFunction to look more like this:

function awesomeFunction() {
  conn('sweet nickname', function(err, sweetResult) {
    alert(sweetResult);
  });
}

Which means, conn needs to accept and use a callback too:

function conn (text, resultsAreInCB){  
  ...
  connection.query(queryString, function(err, rows, fields) {
    var id;
    connection.end();
    if (err){
      resultsAreInCB(err, null);
    } else {
      for (var i in rows) {
        id = rows[i].idUsuario;
      }
      callback(null, id);
    }
  });
  ...
};
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you!, it works perfectly. After test it on my code i understood the way that it works.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.