MySQL的NodeJS函数从表返回错误的值

时间:2019-03-04 17:05:23

标签: javascript mysql node.js html5 async-await

我有一个项目,其中一个函数应该从MySQL表中加载一个值,但是该值始终为FFFFFF,甚至应该是其他值。如果我手动运行函数loadSettings(),它将吐出正确的值

let mysql = require("mysql");
let con = mysql.createConnection({
  host: "localhost",
  user: "root",
  password: ""
});
let set = mysql.createConnection({
   host: "localhost",
   user: "root",
   password: ""
 });
let temp;
let temp2;
con.connect(function(err) {
   if (err) throw err;
  console.log("MySQL: Connected to localhost!");
  let sql = "USE Abschlussprojekt;"
  con.query(sql, function (err, result) {
    if (err) throw err;
    console.log(result);
  });
});
set.connect(function(err) {
 if (err) throw err;
 console.log("MySQL: Connected to localhost!");
 let sql2 = "USE Abschlussprojekt;"
 set.query(sql2, function (err, result) {
   if (err) throw err;
   console.log(result);
 });
});
async function dat2(Befehl) {
  set.query(Befehl, async function (err, result2) {
    if (err) throw err;
    await Sleep(10);
    //console.log(result);
    temp2 = result2;
  });
  return temp2;
}
async function dat(Befehl) {
  con.query(Befehl, async function (err, result) {
    if (err) throw err;
    await Sleep(10);
    //console.log(result);
    temp = result;
  });
  return temp;
}
let hs;
async function loadhighscore()
{
   return await dat('SELECT * FROM highscore;');
}
async function loadSettings()
{
  console.log("Reading from table settings...");
  return await dat2('SELECT * FROM settings;');
}

Main.js

let ohno = await loadSettings(); //new Variable
//await Sleep(100);
console.log("ohno=");
console.log(ohno);

while (ohno == undefined) { //Did the varibale load right?
  delete ohno;
  let ohno = await loadSettings();
  if (ohno != undefined) {
    console.warn("Ohno has the right type"); //Isn't undefined anymore, can continue and break out of loop
    console.log(ohno);
    haha = ohno; //Had to use a new one because ohno always undeclares itself
    break;
  }
  console.warn("ohno = undefined");
  console.log(ohno);
  await Sleep(1000);
}
await Sleep(100);
while (haha == []) { //Is the variable empty?
  console.warn("haha is empty");
  delete haha;
  let haha = await loadSettings();
  await Sleep(1000);
}
console.log(haha);
await Sleep(100);
if (haha[0].DarkMode == "true") {
  DarkMode = true;
}
else if (haha[0].DarkMode == "false") {
  DarkMode = false;
}
else {
  DarkMode = false;
  Fehler(2);
}


console.log('DarkModeColour: ' + haha[0].Farbe);
if (DunklerModusFarbe.indexOf("#") > -1) { // Ist # schon enthalten?
  DunklerModusFarbe = haha[0].Farbe;
}
else {
  DunklerModusFarbe = "#"+haha[0].Farbe;
}

所以它不加载实际值,只是将FFFFFF加载到表以外的任何地方

MySQL

0 个答案:

没有答案