node.js - Node JS : Can't set headers after they are sent -


i have looked @ various answers on site, don't seem able find solution. making 2 identical requests 1 page, 1 of them made every 15 minutes , other 1 done on click. 1 called periodically first 1 called , code follows:

periodic.js

var express = require('express'); var router = express.router(); var mongodb = require('mongodb'); var assert = require('assert');  router.get('/', function(req, res, next) {  var mongoclient = mongodb.mongoclient; var url = 'mongodb://localhost:27017/test';  mongoclient.connect(url, function (err, db) { if (err) {   console.log('unable connect mongodb server. error:', err); } else {    get_number(db,function(user_res){     return res.json(user_res);     db.close();     })    }   }); });      function get_number(db,cb)   {     var collection = db.collection('result');     collection.aggregate(        { $group:           { _id: {'number':'$number','name':'$name'}, total: { $sum: 1 } }         },        { $sort : { total : -1 } },         { $limit : 10 },          function (err, res) {          if (err) console.log(err);          cb(res);        }       );    }  module.exports = router; 

the problem every time call second page, onclick event, data error - "can't set headers after sent" displayed.

the code second page follows:

onclick.js

var express = require('express'); var router = express.router(); var mongodb = require('mongodb');  var assert = require('assert');  router.get('/:number', function(req,res,next){  var mongoclient = mongodb.mongoclient; id = parseint(req.params.number); var url = 'mongodb://localhost:27017/test';  mongoclient.connect(url, function (err, db) {     if (err) {       console.log('unable connect mongodb server. error:', err);     } else {        get_comments(db,function(user_res){         res.json(user_res);         db.close();       })     }   }); });  var get_comments = function(db, callback) {    var cursor = db.collection('result').find({ 'number': id });    cursor.each(function(err, doc) {       assert.equal(err, null);       callback(doc);       if (doc != null) {          callback(doc);       } else {          callback();       }    }); };  module.exports = router; 

both pages being called 1 page only. so, suggestions might going wrong.

the problem lies in cursor.each section. calling callback every item in turn sends individual response item in connection. res.json can used only once request. must restructure code either send data at once or send response using res.write , manually end response after sending data caling res.end()

try changing get_comments following. have edit client uses data because receiving array of objects.

  var get_comments = function(db, callback) {   var cursor = db.collection('gamescore').find({ 'number': id });   var docarr = [];   cursor.each(function(err, doc) {     assert.equal(err, null);     if (doc != null) {       docarr.push(doc);     }     cursor.hasnext().then(function(){       callback(docarr);     });   }); }; 

in case docs returned find query iterated 1 one , pushed temporary array. curosr.hasnext() checks if there more remaining objects in find query. callback called once entire array.


Comments

Popular posts from this blog

Load Balancing in Bluemix using custom domain and DNS SRV records -

oracle - pls-00402 alias required in select list of cursor to avoid duplicate column names -

python - Consider setting $PYTHONHOME to <prefix>[:<exec_prefix>] error -