0

I have a function that queries a mysql database with a select query. This is all inside an async function. The query keeps telling me on the return line that it cannot find the variable "rollStatus".

async function PullRollStatus(){
     return new Promise((resolve,reject)=>{

     var sql = `SELECT * FROM brawlid${brawlID}`
     con.query(sql, (error, rows, fields) => { 

     var rollStatus= []
     for (var i in rows) {
     rollStatus.push(rows[i].Bakuganrolled)
         }

     })
     console.log(rollStatus)
     return rollStatus
     })
 }

     var rolledcheck = await PullRollStatus();
     console.log(rolledcheck)

I've never used new Promise before, as async functions are still kind of new to me. I have tried this without the "return new Promise" line and with it, both giving the same result. I have referenced this async and await on MySQL call in node js and I'm still getting some problems or it might be confusing me more, I don't know. Any help is greatly appreciated. Thanks in advance!

1 Answer 1

1

I would have to know a bit more about the database and the value of brawlID, but you don't use return with promises instead you use resolve and reject, also, you are returning a promise, do you don't use async. Here is an edited example. Note I use mysql.format to pass the variable into the query, this will safeguard against code injection.

Also, I would think you would be using a where statement, unless you have a table for each brawlID, but it would make for sense if brawlID is a column in the table. I changed the function to take the value of brawID passed parameter instead of referencing a global variable.

const mysql = require("mysql2/promise");

const mysqlconfig = {
  host: "localhost",
  user: "youruser",
  password: "yourpassword"
  database: "yourdb"
  multipleStatements: true
};

const con = mysql.createConnection(msqlconfig);

function to (promise) {
    return promise
        .then(val => [null, val])
        .catch(err => [err]);
}

function PullRollStatus(brawlID){
     return new Promise((resolve,reject)=>{

       let sql = `SELECT * FROM brawlid WHERE brawlID=?`;
       mysql.format(sql,brawlID);
       con.query(sql, (error, rows, fields) => { 
         if (error) {
           reject(error);
         } else {
           let rollStatus = [];
           for (let row of rows) {
             rollStatus.push(row.Bakuganrolled)
           }
           console.log(rollStatus);
           resolve(rollStatus);
         }
       });
     });
 }

let brawlIDtoCheck = 1;

let [err,rolledcheck] = await to(PullRollStatus(brawlIDtoCheck));
if (err) {
  console.log("encountered err",err);
}
console.log(rolledcheck)
Sign up to request clarification or add additional context in comments.

7 Comments

Each game has its own ID (aka brawlID) and its own table in my database, so a where statement isn't needed, I believe. I wasn't using mysql2 before this, just mysql, so I did install that and change the require line to match what you provided. Tweaking what you provided considering this, I am now being told Bakuganrolled (a column of my brawlID table) is now coming back as undefined when it was showing correctly before. I have no clue the differences between mysql2 and mysql, so I may need to look into that and update accordingly. Any ideas?
I should also specify that I get the brawlID, which is an 8 digit number that is generated and saved in the channel title in discord. I am also doing all of this in an async function() block.
@Zarvix What I would do is use the Chrome Node JS debugger and put a breakpoint on the line rollStatus.push(rows[i].Bakuganrolled) and see what the content of rows is at that point. That should tell you everything you need to know.
I did do a "console.log(rows)" and a "console.log(rows[0].Bakuganrolled)" right before the loop with the .push line and it shows the two rows I expect, with the column "Bakuganrolled" showing up too along with the correct result for the rows[0] too, so I am unsure why it says that row is undefined when it comes to the .push line in the loop. I'll try the debugger.
Oh, I see why it wasn't working... in the for loop statement "let i of rows" was there instead of "var i of rows". I changed "let" to "var" and I no longer have that problem and it returns exactly what it should! Thanks so much for the help. Your answer solved my problem, short of that one little fix! You're awesome!
|

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.