title: using fetch api function to get data from MongoDB and display them in node.js
body: <p>I have a node application which inserts different types of courses into mongodb and displays them onto the web page directly from the database. i want to use the fetch() api function to get the data from mongodb by passing the data into json format and then displaying them using the ejs engine</p>
  
  <pre><code>mongoose.connect('mongodb://localhost:27017/studentdb')
  var db=mongoose.connection;
  db.on('error', console.log.bind(console, "connection error"));
  db.once('open', function(callback){
      console.log("connection succeeded");
  })
  
  
  app.get('/', function(req, res, next) {
      // fetch and sort course collection by id in descending order
      req.db.collection('course').find().sort({"_id": -1}).toArray(function(err, result) {
          //if (err) return console.log(err)
          if (err) {
              req.flash('error', err)
              res.render('user/list', {
                  title: 'User List',
                  data: ''
              })
          } else {
              // render to views/user/list.ejs template file
              res.render('user/list', {
                  title: 'User List',
                  data: result
              })
          }
      })
  })
  
  // SHOW ADD course FORM
  app.get('/add', function(req, res, next){
      // render to views/user/add.ejs
      res.render('user/add', {
          title: 'Add course',
          topic: '',
          price: '',
          location: '',
          provider:'',
          review_rankings: '',
          author_ranking: ''
      })
  })
  
  
  app.post('/add', function(req,res, next){
      var topic = req.body.topic;
      var price =req.body.price;
      var location = req.body.location;
      var provider = req.body.provider;
      var review_rankings = req.body.review_rankings;
      var author_ranking =req.body.author_ranking;
  
      var data = {
          "topic": topic,
          "price":price,
          "location":location,
          "provider":provider,
          "review_rankings":review_rankings,
          "author_ranking": author_ranking
      };
  
      db.collection('course').insertOne(data,function(err, collection){
  
  
          if (err) throw err;
          console.log("Record inserted Successfully");
  
      });
  
      return res.redirect('/users');
  
  
  
  })
  </code></pre>
  
  <p>I want to use the fetch api function like the code below using mongodb and displaying the information to the web page</p>
  
  <pre><code>fetch('http"//reqres.in./api/users', {
  
      method:'POST',
      headers:{
          'content-type': 'application/json'
      },
  
      body: JSON.stringify({
  
          name:'user 1'
      })
  
  }).then(res =&gt; {
      return res.json()
  
  })
  .then(data =&gt; console.log(data))
      .catch(error =&gt; console.log('ERROR'))
  </code></pre>