title: How to put the results of MySQL database in a variable in Node.js?
body: <p>I have a problem in my node.js application. I'm connecting to the database and getting the data</p>
  
  <pre><code>let users = {};
  let currentUser = "example"; // this variable changes every time a user connects
  connection.query('SELECT * FROM users WHERE name="'+currentUser+'"', function (err, result) {
    if (err) console.log(err);
    users[currentUser] = result[0];
  });
  console.log(users[currentUser]);
  </code></pre>
  
  <p>When I try to <code>console.log</code> the <code>result[0]</code> from inside the function, it returns this:</p>
  
  <pre><code>RowDataPacket {
  n: 11,
  id: 'VKhDKmXF1s',
  name: 'user3',
  status: 'online',
  socketID: 'JbZLNjKQK15ZkzTXAAAB',
  level: 0,
  xp: 0,
  reg_date: 2018-07-16T20:37:45.000Z }
  </code></pre>
  
  <p>I want to put that result from MySQL into <code>users.example</code> or something like that, but when I try the code it returns <code>undefined</code>. So I tried <code>console.log(users[currentUser].id)</code> as an example and it shows an error</p>
  
  <pre><code>TypeError: Cannot read property 'id' of undefined
  </code></pre>
  
  <p>So how do I put the data from the <strong>result</strong> inside my variable <code>users[currentUser]</code>?</p>