title: Variable scope error when attempting to JSON.stringify object after storing said object in Mongo DB
body: <p>Firstly, I would like to address my situation. I am attempting to build a to-do list webapp, in which I save my tasks in a MongoDB, so that I can pull the tasks from the MongoDB so that on the front end the task remains there permanently. The problem is, when I try to convert the object containing the task into a string, so that I can reference it in my client side JS, I get variable scope error (I am assuming) where the object is undefined, and thus I cannot res.end the data to my script.js file. Here is the code</p>
  <pre><code>
  app.get(&quot;/tasks&quot;, (req, res) =&gt; {
    console.log(req.query.task)
    res.send(&quot;hi &quot; + req.query.task)
    MongoClient.connect(url, function(err, db) {
      if (err) throw err
      var dbo = db.db(&quot;Cluster0&quot;)
      
      var myobj = { task: req.query.task }
      
      dbo.collection(&quot;articles&quot;).insertOne(myobj, function(err, res) {
       
        if (err) throw err
        console.log(&quot;1 document inserted&quot;)
        db.close();
        
      })
          var data = JSON.stringify(myobj)
  
      res.end(data);
      console.log(myobj)
      /*dbo.collection(&quot;articles&quot;).deleteMany({}, function(err, obj) {
        if (err) throw err;
        console.log(obj.result.n + &quot; document(s) deleted&quot;);
        db.close();
      });*/
  
  
  
  
    })
    
      
  })
  
  
  
  </code></pre>
  <p>Here are my errors</p>
  <pre><code>ReferenceError: myobj is not defined
      at /home/runner/todolist/index.js:43:29
      at Layer.handle [as handle_request] (/home/runner/todolist/node_modules/express/lib/router/layer.js:95:5)
      at next (/home/runner/todolist/node_modules/express/lib/router/route.js:137:13)
      at Route.dispatch (/home/runner/todolist/node_modules/express/lib/router/route.js:112:3)
      at Layer.handle [as handle_request] (/home/runner/todolist/node_modules/express/lib/router/layer.js:95:5)
      at /home/runner/todolist/node_modules/express/lib/router/index.js:281:22
      at Function.process_params (/home/runner/todolist/node_modules/express/lib/router/index.js:335:12)
      at next (/home/runner/todolist/node_modules/express/lib/router/index.js:275:10)
      at expressInit (/home/runner/todolist/node_modules/express/lib/middleware/init.js:40:5)
      at Layer.handle [as handle_request] (/home/runner/todolist/node_modules/express/lib/router/layer.js:95:5)
  (node:7548) DeprecationWarning: current Server Discovery and Monitoring engine is deprecated, and will be removed in a future version. To use the new Server Discover and Monitoring engine, pass option { useUnifiedTopology: true } to the MongoClient constructor.
  { task: 'sadsda', _id: 5efbe175fef3df1d7c5135ce }
  1 document inserted
   
  </code></pre>
  <p>As you can see, I can indeed console.log(myobj), so I am not sure why it is not able to be converted with JSON.stringify. On a side note, if I am able to successfully parse the object into my client side JS file, will the information stay their permanently, or will it be gone if I refresh the webpage? Any ideas on how I can directly take out data from my MongoDB and store the info in my client side JS so that I can display the To-do tasks permanently?
  I hope I am making myself specific.</p>