Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

105
Views
ExpressJS - Javascript doesn't seem to execute from top-down

I have this script which doesn't seem to run down. I noticed it on the part of the variable reassignment. Code Snippet and output is shown

//Create New Open Market under the game
            let latestRowId = 1;
            var sqlQuery2 = "SELECT ID FROM markets ORDER BY LAST_EDITED DESC LIMIT 1";
            db.query(sqlQuery2, [], function(err, result4) {
                if (err){
                    console.log("Error during /openBetoBetoMarket. Proc_4" + err);
                    res.status(500).json({ msg: "Server Error /openBetoBetoMarket. gameID:" + gameID });
                } else {
                    console.log("result4", result4[0].ID)
                    latestRowId = result4[0].ID
                }
            });
            console.log("latest_row_id", latestRowId);
            
            newRowID = latestRowId + 1
            newMarketId = result[0].MARKET_ID + 1
            newGameId = gameID
            newDescription = gameTitle
            console.log("New row id", newRowID, "new market id", newMarketId)

            sqlQuery = "INSERT INTO markets (ID, MARKET_ID, GAME_ID, DESCRIPTION, LAST_EDITED, WRITER) VALUES (?, ?, ?, ?, NOW(), CURRENT_USER);"
            db.query(sqlQuery, [newRowID, newMarketId, newGameId, newDescription], (err, result4) => {
                if (err){
                    console.log("Error during /openBetoBetoMarket. Proc_5 " + err);
                    res.status(500).json({ msg: "Server Error /openBetoBetoMarket. gameID:" + gameID });
                } else if (result4.affectedRows > 0){
                    console.log("Created new open market for /openBetoBetoMarket. MarketID: " + newMarketId);
                    res.status(200).json({msg: "Created New Open Market", marketID: newMarketId, isOpen: 1})     
                } else {
                    console.log("Error during /openBetoBetoMarket. Proc_5. Inserted but no affected Rows");
                    res.status(500).json({ msg: "Server Error /openBetoBetoMarket. gameID:" + gameID });
                }
            })

Output is shown below:

latest_row_id 1
New row id 2 new market id 3
result4 2
Error during /openBetoBetoMarket. Proc_5 Error: ER_DUP_ENTRY: Duplicate entry '2' for key 'markets.PRIMARY'

As you can see, it seems to skip the first db.query and goes straight to the next variable assignment then executes the first db.query. Any Idea what im missing here? First time posting here. Thanks

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The way you're defining (or simply not defining at all) your variables is leading you into trouble.

First of all, ditch the var keyword. There is absolutely no reason to use it. var defines variables in the global scope and allows them to be reassigned. You really never should use global variables except for the smallest of scripts that will never be significantly altered. There are very few instances where you are going to actually want a variable to be reassigned, but if you do, use the let keyword because then you at least have block level scoping.

Second, halfway down you have four variables incorrectly defined without any keyword at all. It is not proper JavaScript syntax to define a variable as follows:

// wrong! this is correct in Python but not JS

varName = <value>;

You need to use one of let, const or var. const is recommended.

Re-write your code to use proper variable definition and see if you can write your code in a way that does not reassign variables for bonus points. You should then find that your code doesn't produce unexpected results.

Best of luck

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!