4

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
user_1234
  • 741
  • 1
  • 9
  • 22
  • 1
    When you create the constant you need to initialize it using `=`. You can't declare it and then assign a value to it at a later point, for that you would need `let` (see this [MDN article](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Errors/Missing_initializer_in_const)). 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 reference](https://stackoverflow.com/q/23667086) – Nick Parsons Dec 30 '21 at 10:51
  • @NickParsons can u please update in comment . – user_1234 Dec 30 '21 at 10:52
  • Sorry, not quite sure what you mean by "update in comment"? – Nick Parsons Dec 30 '21 at 10:53
  • @NickParsons your answer or code, can u please update it, because after using let I am getting undefined value. – user_1234 Dec 30 '21 at 10:56
  • The reason you are getting `undefined` is explained in the second link I have shared. – Nick Parsons Dec 30 '21 at 10:57
  • @NickParsons saw that link it is little bit difficult to understand, can you please mention here few lines – user_1234 Dec 30 '21 at 11:01
  • 1
    `console.log('::'+ totalRecords);` runs before the code in your callback function: `totalRecords = rows.length`. So that is why `totalRecords` is `undefined` when you log it. Move `console.log('::'+ totalRecords);` and any other code that relies on `rows` inside of your callback function. – Nick Parsons Dec 30 '21 at 11:04
  • @NickParsons actually I want that value should be accessed any where for that reason i am checking outside. – user_1234 Dec 30 '21 at 11:07
  • 1
    Please also read: [How to return the response from an asynchronous call](https://stackoverflow.com/a/14220323) it looks like a big read, but 100% required knowledge if you want to write a javascript program that deals with asynchronous code (which is what you're trying to do). There is no clear-cut answer to your problem as more context would be needed to get a better idea for what the best option is. I suggest you have a read of the links I've sent to try and get a better idea, you may be able to use Promises here to help, which is also explained the link – Nick Parsons Dec 30 '21 at 11:12
  • @NickParsons i have read the link which you have shared, but it is too complicated to integrate, just wanted to ask can you show with one example how to get mysql result outside of the function – user_1234 Dec 30 '21 at 11:51

2 Answers2

5

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'

p4avinash
  • 548
  • 2
  • 4
  • 15
3

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

Indraraj26
  • 1,726
  • 1
  • 14
  • 29