title: SyntaxError: await is only valid in async function, When connecting to Mongo DB using Node JS
body: <p>I am trying to access a mongo database using an async / await function in Javascript using the code provided below. When I run the code, the terminal returns the following error:</p>
  
  <pre><code>SyntaxError: await is only valid in async function
  </code></pre>
  
  <p>The error is confusing to me, because of my use of "async" for newFunction. I have tried changing the location of "async" and "await," but no combination that I have tried so far has yielded successful execution. Any insight would be very much appreciated.</p>
  
  <pre><code>var theNames;
  var url = 'mongodb://localhost:27017/node-demo';
  
  const newFunction = async () =&gt; {
        MongoClient.connect(url, function (err, db) {
        if (err) throw err;
        var dbo = db.db("node-demo");
        //Find the first document in the customers collection:
        dbo.collection("users").find({}).toArray(function (err, result) {
          if (err) throw err;
          theNames = await result;
          return theNames;
          db.close();
        });
      });
  }
  
  newFunction();
  console.log(`Here is a list of theNames: ${theNames}`);
  </code></pre>