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
=. You can't declare it and then assign a value to it at a later point, for that you would needlet(see this MDN article). Note that it also looks like your code will face the following issue: Why is my variable unaltered after I modify it inside of a function? - Asynchronous code referenceconsole.log('::'+ totalRecords);runs before the code in your callback function:totalRecords = rows.length. So that is whytotalRecordsisundefinedwhen you log it. Moveconsole.log('::'+ totalRecords);and any other code that relies onrowsinside of your callback function.