Home > database >  Missing initializer in const declaration in node js
Missing initializer in const declaration in node js

Time:12-31

I am writing mysql query and kept those records in variable, My requirement is to get access that variable in anywhere so declared it outside of route,but it is showing error like : SyntaxError: Missing initializer in const declaration

const totalQuery = "select name from users";
   
    const totalRecords;
    dbConn.query(totalQuery,function(err,rows)     {
        totalRecords = rows.length
      
    })
    console.log('::'  totalRecords);
    process.exit();

Error:

SyntaxError: Missing initializer in const declaration

CodePudding user response:

Make it let instead of constas you are re-assigning the totalRecords variable

CodePudding user response:

In javascript, you can't assign value to 'const' later after it's declaration. You're bound to assign the value to const on the declaration line.

If you really need to declare the value some where later, then better go with 'let'

  • Related