Skip to content

Node.js based forum software built for the modern web

updated getPostsByPids method to resolve premature return issue, and refactored it to use async.map instead of eachSeries

Julian Lam4cea3130605f
diff --git a/src/posts.js b/src/posts.js
index d3a804acf6..29989fb8e2 100644
--- a/src/posts.js
+++ b/src/posts.js
@@ -19,7 +19,6 @@ var RDB = require('./redis.js'),
Posts.getPostsByTid = function(tid, start, end, callback) {
RDB.lrange('tid:' + tid + ':posts', start, end, function(err, pids) {
-
RDB.handle(err);
if (pids.length) {
@@ -154,28 +153,23 @@ var RDB = require('./redis.js'),
RDB.hset('post:' + pid, field, value);
}
-
-
-
- /* getPostsByPids using redis's multi pipeline */
Posts.getPostsByPids = function(pids, callback) {
- var posts = []
- var multi = RDB.multi();
+ var posts = [],
+ multi = RDB.multi();
- for (v in pids) {
- var _pid = pids[v]
- multi.hgetall("post:"+_pid);
+ for(var x=0,numPids=pids.length;x<numPids;x++) {
+ multi.hgetall("post:"+pids[x]);
}
multi.exec(function (err, replies) {
- async.eachSeries(replies, function(postData, _callback) {
- if(postData) {
+ async.map(replies, function(postData, _callback) {
+ if (postData) {
postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
postData.post_rep = postData.reputation;
postData['edited-class'] = postData.editor !== '' ? '' : 'none';
postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
- if(postData.uploadedImages) {
+ if (postData.uploadedImages) {
try {
postData.uploadedImages = JSON.parse(postData.uploadedImages);
} catch(err) {
@@ -188,15 +182,13 @@ var RDB = require('./redis.js'),
postTools.parse(postData.content, function(err, content) {
postData.content = content;
- posts.push(postData);
+ _callback(null, postData);
});
- return _callback(null)
- }
- else {
- return _callback(null)
+ } else {
+ _callback(null);
}
- }, function(err) {
- if(!err) {
+ }, function(err, posts) {
+ if (!err) {
return callback(null, posts);
} else {
return callback(err, null);
@@ -205,37 +197,6 @@ var RDB = require('./redis.js'),
})
}
-
-
-
-
- Posts.getPostsByPids_original = function(pids, callback) {
- var posts = [];
-
- async.eachSeries(pids, function(pid, callback) {
- Posts.getPostData(pid, function(postData) {
- if (postData) {
- postData.relativeTime = new Date(parseInt(postData.timestamp,10)).toISOString();
- postData.post_rep = postData.reputation;
- postData['edited-class'] = postData.editor !== '' ? '' : 'none';
- postData['relativeEditTime'] = postData.edited !== '0' ? (new Date(parseInt(postData.edited,10)).toISOString()) : '';
-
- postTools.parse(postData.content, function(err, content) {
- postData.content = content;
- posts.push(postData);
- callback(null);
- });
- }
- });
- }, function(err) {
- if (!err) {
- callback(null, posts);
- } else {
- callback(err, null);
- }
- });
- }
-
Posts.get_cid_by_pid = function(pid, callback) {
Posts.getPostField(pid, 'tid', function(tid) {
if (tid) {