const { ObjectID } = require('mongodb');

module.exports = (app, database) => {
  const db = database.collection('notes');

  app.get('/notes/:id', (req, res) => {
    const { id } = req.params;
    console.log(id);
    const query = { _id: new ObjectID(id) };
    console.log(query);
    db
      .findOne(query)
      .then(result => res.send(result))
      .catch((error) => {
        console.log(error);
        res.send({ error });
      });
  });

  app.post('/notes', (req, res) => {
    const { title, body } = req.body;
    const note = {
      title,
      body,
    };

    db.insert(note, (err, result) => {
      if (err) {
        res.send({ error: 'An error occurred' });
      } else {
        res.send(result.ops[0]);
      }
    });
  });
};

@@ -577,64 +577,154 @@
  %7D;%0A
-%0A    db.insert(note, (err, result) =%3E %7B%0A      if 
+    console.log(note);%0A    db%0A      .insert(note)%0A      .then(result =%3E res.send(result.ops%5B0%5D))%0A      .catch((err) =%3E %7B%0A        console.log
 (err)
- %7B
+;
 %0A   
@@ -776,62 +776,8 @@
 ;%0A  
-    %7D else %7B%0A        res.send(result.ops%5B0%5D);%0A      %7D%0A
     

change to use Promisse style