Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
169 views
in Technique[技术] by (71.8m points)

Issue with MySQL Databank not loading in time (javascript)

I am currently trying to make a bot for discord, but im running in a issue. Whenever i try to load some data from my mysql database, it just does not safe in my variable. Here is the part of the code:

    function loadpf() {
    const plaintxt = `test`
    async function getFieldFromSQL() {
    await con.query(`SELECT field FROM gamefield WHERE userid = '${userid}'`, function (err, results) {
      if(results[0] === undefined) {
        console.log(`FATAL ERROR: Game could not be loaded.`);
      }
      else {
        console.log(results[0].field)
        return results[0].field;
      }
    });
  }
  console.log(plaintxt)

Might someone tell me where my mistake is at? Also: When having a look at my terminal, i also can see that "console.log(results[0].field)" is run like a second after the rest of the code is done, which lets me guess that the code is not waiting for the data to be returned from my database. Does someone know how to wait for that?

Thanks in advance.

question from:https://stackoverflow.com/questions/65902232/issue-with-mysql-databank-not-loading-in-time-javascript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...