Saturday, 29 April 2017

holedata is not defined while loading ejs file after res.render

I am inserting the data into mongodb and then page rendering to success.ejs. In the success.ejs file I gave one link. When click link in the same success.js file data should fetch from mongodb and show in same success.ejs file. But before loading the success.ejs file after post the data in the console showing holedata is not defined error message.

My post data part in index.js file

app.post('/signingin', function(req, res) {
      var item = {
        name: req.body.name,
        title: req.body.title,
        age: req.body.age
      };
      mongo.connect(url, function(err, db){
        assert.equal(null, err);
        db.collection('satyamsoft').insertOne(item, function(err, result){
          assert.equal(null, err);
          console.log('item inserted');
          db.close();
        });
      });
        res.render('pages/success');
        //console.log(req.body);
    });

My success.ejs page

!DOCTYPE html>
<html lang="en">

    <% include ../partials/head %>
    <title>Post Jobs | Spyeer</title>
    <body>

        <% include ../partials/nav %>       
        <section class="get">
          <h2>Data Inserted</h2>
          <a href="/getdata">Get Data</a>
          <div>
            <ul>
                <% holedata.forEach(function(data) { %>
                    <li><%= data.this.name %> - <%= data.this.title %> - <%= data.this.age %></li>
                <% }); %>
            </ul>
          </div>
        </section>

        <% include ../partials/footer %>

        <% include ../partials/scripts %>

    </body>
</html>

get part code in index.js file

app.get('/getdata', function(req, res){
      var resultArray = [];
      mongo.connect(url, function(err, db){
        assert.equal(null, err);
        var cursor = db.collection('satyamsoft').find();
        cursor.forEach(function(doc, err){
          assert.equal(null, err);
          resultArray.push(doc);
          //console.log(resultArray);
        //res.render('pages/getdata', {holedata: resultArray});
      }, function(){
        db.close();
        res.render('pages/success', {holedata: resultArray});
      });

Please help me advance thanks.



via Satyam

No comments:

Post a Comment