mongodb - Node.js Streams on(end) completing before asynchronous on(readable) completed -
i'm using node.js request library along node-feedparser capture feed , it's posts mongodb database using mongoose.
i'm storing post info posts collection , feed info feeds collection, need store post._id within array in feeds collection called feeds._post.
the problem i'm having using stream interface, feedparser.on('end') called before of feedparser.on('readable')'s async calls db have completed, hence end 15 posts in post collection , 11 post._id's in feed._post array.
i understand if plain javascript, utilize async ensure .on('end') waits .on('readable')'s complete, how go streams?
thanks in advance.
db.feed.findbyid(feedid, function(error, feed) { // request.on('response') -> this.pipe(feedparser) feedparser.on('readable', function() { var post; while (null !== (post = this.read())) { db.post.create({ /* post details */ }, function(err, post) { feed._post.push(post); }); } }); feedparser.on('end', function() { feed.save(); }); });
you'll need maintain track of counter , boolean. increment counter when "readable" event first fires , decrement counter when you're finished saving database. boolean starts out in 1 state , switched when "end" event fires. example:
var processing = 0, done = false; var finished = function(){ if(processing === 0 && done){ feed.save(); // ... other stuff } }; feedparser.on("readable", function(){ processing++; dostuff(something, function(){ // asynchronous processing--; finished(); }); }); feedparser.on("end", function(){ done = true; finished(); }); node.js mongodb asynchronous stream mongoose
No comments:
Post a Comment