0

I'm trying to assign a numerical value to my var gameid with the function initGame(red, black options).

unfortunately gameid will always return undefined.

async function startPendingRoom (red, black) {
    var gameid = initGame (red, black, returnPendingRoomIfExists (red).options);
    removePendingRoomIfExists (red);
    removePendingRoomIfExists (black);

    io.to (red).emit ('startOnlineGameRES', { gameid : gameid });
    io.to (black).emit ('startOnlineGameRES' , {  gameid : gameid });
    
    console.log (red," vs. ", black, " gameid: ", gameid);
    updatePendingRoomsCLIENT ();
}


function initGame (red, black, options, gameid) {
    dbCon.connect(function(err) { if (err) throw err;
        dbCon.query("INSERT INTO options "
            +"VALUES ("
            +"0 ,"
            + options.malusSize + ", "
            + options.sequenceSize +", "
            + options.throwOnWaste +", "
            + options.throwOnMalus +", "
            + "'"+options.variant+"'" +", "
            + options.turnsTimed +", "
            + options.timePerTurn +", "
            + options.roundsTimed +", "
            + options.timePerRound +", "
            + (options.roomName     != "" ? "'"+options.roomName+"'"     : "null" )+", "
            + (options.roomPassword != "" ? "'"+options.roomPassword+"'" : "null" )+");", 
        function (err, option) { if (err) throw err;
            dbCon.query("INSERT INTO games "
                +"VALUES ("
                +"0 ,"
                + option.insertId + ", "
                + "'"+red+"'" + ", "
                + "'"+black+"'" +");", 
            function (err, game) { if (err) throw err;
                return game.insertId;
            });
        });
    });
    
}
  • This looks like async callback issue. try `var gameid = await initGame (red, black, returnPendingRoomIfExists (red).options);` – vanowm Aug 11 '21 at 19:42
  • Unfortunately this doesn't fix the problem. VSC notifies "'await' has no effect on the type of this expression" –  Aug 11 '21 at 19:49
  • In that case you'll need modify initGame to accept 5th parameter as callback and call it once the value is received. – vanowm Aug 11 '21 at 19:51

1 Answers1

0

Try this

function initGame (red, black, options, gameid) {
    return new Promise((resolve) => {
      dbCon.connect(function(err) { if (err) throw err;
        dbCon.query("INSERT INTO options "
            +"VALUES ("
            +"0 ,"
            + options.malusSize + ", "
            + options.sequenceSize +", "
            + options.throwOnWaste +", "
            + options.throwOnMalus +", "
            + "'"+options.variant+"'" +", "
            + options.turnsTimed +", "
            + options.timePerTurn +", "
            + options.roundsTimed +", "
            + options.timePerRound +", "
            + (options.roomName     != "" ? "'"+options.roomName+"'"     : "null" )+", "
            + (options.roomPassword != "" ? "'"+options.roomPassword+"'" : "null" )+");", 
          function (err, option) { 
              if (err) throw err;
              dbCon.query("INSERT INTO games "
                  +"VALUES ("
                  +"0 ,"
                  + option.insertId + ", "
                  + "'"+red+"'" + ", "
                  + "'"+black+"'" +");", 
              function (err, game) { 
                  if (err) throw err;
                  resolve(game.insertId);
              });
          });
      });
    })
}

const gameId = await initGame(...);
  • Don't just post code. Explain what the problem is, and how you fixed it. – Barmar Aug 11 '21 at 20:17
  • thank you so much. you fixed my problem and taught me in a practical example how to use promises. –  Aug 11 '21 at 20:19