Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing initializer in const declaration in node js

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
like image 543
user_1234 Avatar asked Sep 08 '26 15:09

user_1234


2 Answers

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'

like image 170
p4avinash Avatar answered Sep 10 '26 03:09

p4avinash


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

like image 31
Indraraj26 Avatar answered Sep 10 '26 05:09

Indraraj26



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!