Wednesday, 15 July 2015

node.js - Bluebird promise-based request handler in Express returning error -



node.js - Bluebird promise-based request handler in Express returning error -

i'm using express (4.x), redis (2.8) , bluebird (2.x) - needed string multiple redis calls homecoming response:

var promise = require('bluebird'); var redis = require('redis'); var redis = redis.createclient(6379,process.env["redis_endpoint"],{}); promise.promisifyall(redis); // exports.inoffers = function (req, res) { homecoming redis.smembersasync('advertisers') .map(function(advid){ console.log('advid',advid); homecoming redis.smembersasync('advertiser:'+advid+':inoffers') .map(function(inofferid){ console.log('offerid: ',inofferid); homecoming redis.hgetallasync('advertiser:'+advid+':inoffer:'+inofferid); }) }) .done(function(inoffers){ console.log('inoffers: ',inoffers); res.json({inoffers: inoffers}) }) .catch(function(err){ console.log((new date).toutcstring()+" [error] ", err); res.writehead(500); res.end(); }); };

according logs, info gathered correctly redis, next error, instead of response:

/var/app/current/node_modules/bluebird/js/main/async.js:95 throw res.e; ^ error: can't set headers after sent.

any ideas? i'm new bluebird, messed up...

to clarify, , benjamin pointed out below, issue trying set headers on response object after has been sent client. i'm not sure if there other middleware returning response, or if .done/.catch have there. seek code below end of promise chain, although i'm not sure help. tip - per bluebird docs, don't need utilize 'done', although can if want (bluebird .done docs). if utilize .done, should lastly thing in promise chain (although can have both fulfilled , rejected handlers on it, .then).

.done(function(inoffers){ console.log('inoffers: ',inoffers); res.json({inoffers: inoffers}) }, function(err){ console.log((new date).toutcstring()+" [error] ", err); res.writehead(500); res.end(); });

node.js express redis promise bluebird

No comments:

Post a Comment