Thursday, 11 May 2017

Error: Can't set headers after they are sent--debugged but still getting it

  • I have been trying to develop a web page which enables to render the list of cars enrolled for a particular event from the database by clicking the event name.
  • Earlier, I have created a get function /get-data in index.js under route directory (folder) which renders list of all the cars enrolled for the event.
  • Then, I have created another function /music which renders list of cars who got enrolled for music event. To render the student list, I have used mongodb dommand var cursorName = db.collection('user-data').find( { FirstName: "toyota" } );
  • After clicking the music link, or typing the URL to view the list, localhost server gets disconnected and the error coming as Error: Can't set headers after they are sent.
  • I would like to know whether the both the route function (/get-data and /music) can be done in the single index.js file
  • I have followed the below link to develop the web application
  • Link: https://github.com/mschwarzmueller/nodejs-basics-tutorial/tree/master/09-mongodb
  • Created route function named /music in index.js under route directory is provided below:
  • Kindly provide the solution for the scenario.
router.get('/music', function(req, res, next) {
  var resultArray = [];
  mongo.connect(url, function(err, db) {
    assert.equal(null, err);
    var cursor = db.collection('user-data').find((SportingEvent) where Age = "Toyota");
    cursor.forEach(function(doc, err) {
      assert.equal(null, err);
      resultArray.push(doc);
    }, function() {
      db.close();
      res.render('data', {items: resultArray});
    });
  });
});

/get-data

router.get('/get-data', function(req, res, next) {
  var resultArray = [];
   var resultArrayName = [];

  mongo.connect(url, function(err, db) {
    assert.equal(null, err);
    var cursor = db.collection('user-data').find();

    //var cursorName = db.collection('user-data').find( { FirstName: "toyota" } );

    cursor.forEach(function(doc, err) {
      assert.equal(null, err);
      resultArray.push(doc);
    }, function() {
      db.close();
      res.render('data', {items: resultArray});
      //console.log(resultArray);
    });
  });
});

music.hbs
    <a href="/music">LOAD DATA</a>
    <div>
        <article>
            <table>
                <tr>
                    <th>Name1</th>
                    <th>Name2</th>
                    <th>Age</th>
                    <th>Gender</th>
                    <th>Event</th>
                    <th>Category</th>
                    <th>Size</th>
                    <th>StudentID</th>
                    <th>Contact</th>
                    <th>EmailID</th>
                    <th>ID</th>
                </tr>
                
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                



            </table>
        </article>
    </div>

data.hbs
<a href="/get-data">LOAD DATA</a>
    <div>
        <article>
            <table>
                <tr>
                    <th>Name1</th>
                    <th>Name2</th>
                    <th>Age</th>
                    <th>Gender</th>
                    <th>Event</th>
                    <th>Category</th>
                    <th>Size</th>
                    <th>StudentID</th>
                    <th>Contact</th>
                    <th>EmailID</th>
                    <th>ID</th>
                </tr>
                
                    <tr>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                        <td></td>
                    </tr>
                


Error


Error: Can't set headers after they are sent.
    at ServerResponse.OutgoingMessage.setHeader (_http_outgoing.js:356:11)
    at ServerResponse.header (C:\Users\Downloads\nodejs-basics-tutorial-master\09-mongodb\node_modules\express\lib\response.js:718:10)
    at ServerResponse.contentType (C:\Users\Downloads\nodejs-basics-tutorial-master\09-mongodb\node_modules\express\lib\response.js:551:15)
    at ServerResponse.send (C:\Users\Downloads\nodejs-basics-tutorial-master\09-mongodb\node_modules\express\lib\response.js:138:14)
    at done (C:\Users\Downloads\nodejs-basics-tutorial-master\09-mongodb\node_modules\express\lib\response.js:957:10)
    at Immediate.<anonymous> (C:\Users\Downloads\nodejs-basics-tutorial-master\09-mongodb\node_modules\express-handlebars\lib\utils.js:26:13)
    at runCallback (timers.js:649:20)
    at tryOnImmediate (timers.js:622:5)
    at processImmediate [as _immediateCallback] (timers.js:594:5)
npm ERR! code ELIFECYCLE
npm ERR! errno 1
npm ERR! 05-express-first-app@0.0.0 start: `node ./bin/www`
npm ERR! Exit status 1
npm ERR!
npm ERR! Failed at the 05-express-first-app@0.0.0 start script 'node ./bin/www'.
npm ERR! Make sure you have the latest version of node.js and npm installed.
npm ERR! If you do, this is most likely a problem with the 05-express-first-app package,
npm ERR! not with npm itself.
npm ERR! Tell the author that this fails on your system:
npm ERR!     node ./bin/www
npm ERR! You can get information on how to open an issue for this project with:
npm ERR!     npm bugs 05-express-first-app
npm ERR! Or if that isn't available, you can get their info via:
npm ERR!     npm owner ls 05-express-first-app
npm ERR! There is likely additional logging output above.



via texi rv

No comments:

Post a Comment