0

Hi,

I have a mysql query like this:

con.query("SELECT name FROM members WHERE id=1", function(err, rows, fields) {
   console.log(rows); 
});

the problem with this is that it forces me to put pretty much ALL my code inside this function in order to get the value from rows where I want it. Isnt there any other way more simple? Like store the value into a variable so I can use it later as many times as I need it? Like this:

var name = con.query("SELECT name FROM members WHERE id=1");

some function() {
  var value = name;
  return stuff;
}
console.log(name); 

or so?

Thank you.

Cain Nuke
  • 2,843
  • 5
  • 42
  • 65
  • Javascript is asynchronous, that mean there's always callback function. Or from ES6, we have promise, then we can reduce callback by using async / await. Read js document. – Trần Đức Huy Sep 19 '21 at 01:09
  • do you care to elaborate? – Cain Nuke Sep 19 '21 at 02:23
  • Does this answer your question? [How to return the response from an asynchronous call](https://stackoverflow.com/questions/14220321/how-to-return-the-response-from-an-asynchronous-call) – David Sep 20 '21 at 20:39
  • no, sorry. I dont know if you are the same David that answered my other question but we had already acknowledged it was impossible to declare a variable outside the function. – Cain Nuke Sep 20 '21 at 20:50

2 Answers2

0

An option would be something along the lines of:

let nameVar = ""

connection.query("SELECT name FROM members WHERE id=1", function(err, rows, fields){
  if(err) {
    throw err;
  } else {
    setName(rows);
  }
});

function setName(value) {
  nameVar = value;
  console.log(nameVar);
}
  • I think it would be the same thing. It requires me to put everything inside a function, in this case `setname()` isnt? – Cain Nuke Sep 19 '21 at 01:02
  • @CainNuke How I saw it was this way you a declaring the variable in a way that doesn't tie it to the query itself and you just use the query to set the name of the variable that can be then called lated without sending another query. Though I may have misunderstood! – Justin.Arnold Sep 19 '21 at 01:07
  • Okay, but lets say for example I need the variable also inside a function called emitmsg(); how would you do it? – Cain Nuke Sep 19 '21 at 01:10
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-ask). – Community Sep 19 '21 at 01:11
  • @CainNuke By 'the variable do you mean the name for, the query? why would you have a hard time accessing it in another function? It would be out of function scope and able to accessed. – Justin.Arnold Sep 19 '21 at 01:25
  • Yep, I dont want to have to write the entire function every time I need the query. – Cain Nuke Sep 19 '21 at 02:24
  • @CainNuke Do you mean every time you need the last result? Because you could access the name with just ```nameVar```. If you mean to rerun the query then you just need to put that in its own function and call its name. no need to type out the function again. – Justin.Arnold Sep 19 '21 at 03:28
  • you mean that if I use your code and have a function like `function something() { console.log(nameVar); }` I will get the value I am expecting? – Cain Nuke Sep 19 '21 at 03:55
0

What you want is a function that returns the response from the SQL query. A way to do this is to create a Promise and use async/await to get the response.

Example:

getName(){
    const promise = new Promise((resolve, reject) => {
        conn.query("SELECT name FROM members WHERE id=1", (err, res, fields) => {
            if (err) reject(err);

            resolve(res);
        });
    });
}

func() async {

    try{
        const data = await getName();
        console.log(data);
    } catch(e) {
        console.log(e);
    }
}