Reply.find({topic_id: id}, '', {sort: 'create_at'}, function (err, replies) { if (err) { return cb(err); } if (replies.length === 0) { return cb(null, []); } var proxy = new EventProxy(); proxy.after('reply_find', replies.length, function () { cb(null, replies); }); for (var j = 0; j < replies.length; j++) { (function (i) { var author_id = replies[i].author_id; User.getUserById(author_id, function (err, author) { if (err) { return cb(err); } replies[i].author = author || { _id: '' }; replies[i].friendly_create_at = tools.formatDate(replies[i].create_at, true); if (replies[i].content_is_html) { return proxy.emit('reply_find'); } at.linkUsers(replies[i].content, function (err, str) { if (err) { return cb(err); } replies[i].content = str; proxy.emit('reply_find'); }); }); })(j); } });
it('should send message to all mention users', function (done) { done = pedding(done, 2); var atUserIds = [String(adminUser._id), String(normalUser2._id)]; var ep = new eventproxy(); ep.after('user_id', atUserIds.length, function (user_ids) { user_ids.sort().should.eql(atUserIds.sort()); done(); }); mm(message, 'sendAtMessage', function (atUserId, authorId, topicId, replyId, callback) { // String(atUserId).should.equal(String(atUserIds[count++])); ep.emit('user_id', String(atUserId)); callback(); }); var text = '@' + adminUser.loginname + ' @' + normalUser2.loginname + ' @notexitstuser 你们好'; at.sendMessageToMentionUsers(text, testTopic._id, normalUser._id, function (err) { should.not.exist(err); done(); }); });
client.smembers('rooms:' + room_key + ':online', function(err, online_users) { if(err) return cb(err); var users = []; var proxy = new EventProxy(); proxy.after('user_ready',online_users.length, function(){ return cb(null, users); }) online_users.forEach(function(userKey, index) { client.get('users:' + userKey + ':status', function(err, status) { if(err) return cb(err); var msnData = userKey.split(':') , username = msnData.length > 1 ? msnData[1] : msnData[0] , provider = msnData.length > 1 ? msnData[0] : "twitter"; users.push({ username: username, provider: provider, status: status || 'available' }); proxy.emit('user_ready'); }); }); // return cb(null,users); });
}).toArray(function(err, result) { if (err) return callback(errInfo); var de = new EventProxy(); de.after('documentRollback', result.length, ce.emit.bind(ce, 'collectionRollback')); de.fail(ce.emit.bind(ce, 'error')); result.forEach(handler.bind(null, name, de.done('documentRollback'))); });
eventProxy : function(){ var ep = new eventproxy(); ep.after('getBook_html', booklist.length,function (booklist) { booklist = booklist.map(function (book) { }); }); }
}, function(err, indScore) { if (err) { return callback(err); } var ep2 = new EventProxy(); ep2.fail(callback); ep2.after('EOIndicateAverageScores.findOne', indGroup.indicators.length, function(list) { Logger.debug('[createReport#EOIndicateAverageScores.findOne] result: ', list); var totalScore = 0; // list = _.compact(list); list.forEach(function(item) { totalScore += (item.score || 0); }); Logger.debug('[>>>> indGroup._id >>>>]', teacher.id, indGroup._id, totalScore); result.scores[indGroup._id] = { totalScore: totalScore, // 加权得分: 例如,指标组总分满分100,指标组权重30。 // 某人指标组总分得分80,他的加权得分:(80/100)×30=24。 // 同组加权平均分: 是指同一个用户组的所有用户的加权得分的平均分。 weightedScore: (totalScore / indGroup.score) * indGroup.weight, list: list }; ep.group('indGroups.forEach')(); }); indGroup.indicators.forEach(function(ind) { // gatherType 1: 文件导入, 2: 互评平均分, 3: 生评平均分 var score = indScore ? indScore.scores[ind._id] : { score: 0, indicator: ind }; if (score.toObject) { score = score.toObject(); } if (ind.gatherType === 2 || ind.gatherType === 3) { db.EOIndicateAverageScores.findOne({ term: indGroup.term, type: ind.gatherType === 2 ? 0 : 1, appraiseeId: teacher.id }, ep2.group('EOIndicateAverageScores.findOne', function(doc) { if (doc && doc.averageScore) { score.score = parseFloat(doc.averageScore.toFixed(2)); } else { score.score = 0; } return score; })); } else { ep2.group('EOIndicateAverageScores.findOne')(null, score); } }); }); // end IndicatorScores.findOne
exports.index = function (req, res, next) { // for(var i=1 ; i<= pageNum ; i++){ // pageUrls.push('http://www.cnblogs.com/#p'+i); // } //改为 http://www.cnblogs.com/?CategoryId=808&CategoryType=%22SiteHome%22&ItemListActionName=%22PostList%22&PageIndex=15&ParentCategoryId=0#p11 for (var i = 1; i <= pageNum; i++) { pageUrls.push(cnblogUrl + '?CategoryId=808&CategoryType=%22SiteHome%22&ItemListActionName=%22PostList%22&PageIndex=' + i + '&ParentCategoryId=0'); } pageUrls.forEach(function (pageUrl) { superagent.get(pageUrl) .end(function (err, pres) { // pres.text 里面存储着请求返回的 html 内容,将它传给 cheerio.load 之后 // 就可以得到一个实现了 jquery 接口的变量,我们习惯性地将它命名为 `$` // 剩下就都是利用$ 使用 jquery 的语法了 var $ = cheerio.load(pres.text); $('.post_item').each(function (idx, element) { var $element = $(element); var href = $element.find('.titlelnk').attr('href'); var title = $element.find('.titlelnk').text(); var userImg = $element.find('.pfs').attr('src'); var userName = $element.find('.post_item_foot a').eq(0).text(); var time = $element.find('.post_item_foot').text().match(/[\d|\-|\:]/g).join('').substring(0, 15); articles.push({ title: title, href: href, userImg: userImg, userName: userName, time: time }); // 相当于一个计数器 ep.emit() 来告诉 ep 自己,某某事件已经完成了。 ep.emit('BlogArticleHtml', href); }); }); }); // 命令 ep 重复监听 pageUrls.length * 20 次 `BlogArticleHtml` 事件再行动 ep.after('BlogArticleHtml', pageUrls.length * 20, function (articleUrls) { // articleUrls 是个数组,包含了 40 次 ep.emit('BlogArticleHtml', href) 中的那 pageUrls.length * 20 个 href // 当所有 'BlogArticleHtml' 事件完成后的回调触发下面事件 // ... //console.log('******************* ep.after ********************'); //console.log('articleUrls : ', articleUrls); res.render('spider-cnblogs-list', { title: '爬虫-cnblogs', signature: '白马山庄', articles: articles }); }); };
.end(function (err, sres) { if (err) { return next(err); } var topicUrls = []; var $ = cheerio.load(sres.text); $('#topic_list .topic_title').each(function (idx, element) { var $element = $(element); var href = url.resolve(cnodeUrl, $element.attr('href')); topicUrls.push(href); }); var ep = new eventproxy(); ep.after('topic_html',topicUrls.length, function(topics){ topics = topics.map(function(topicPair){ var topicUrl = topicPair[0]; var topicHtml = topicPair[1]; var $ = cheerio.load(topicHtml); return ({ title: $('.topic_full_title').text().trim(), href: topicUrl, comment1: $('.reply_content').eq(0).text().trim() }); }); console.log('final:'); console.log(topics); res.render('crawler', { datas: topics }); }); var concurrencyCount = 0; var fetchUrl = function (url, callback) { var delay = parseInt((Math.random() * 10000000) % 2000, 10); concurrencyCount++; console.log('现在的并发数是', concurrencyCount, ',正在抓取的是', url, ',耗时' + delay + '毫秒'); superagent.get(url) .end(function(err, res){ console.log('fetch ' + url + ' successful'); ep.emit('topic_html',[url, res.text]); }); setTimeout(function() { concurrencyCount--; callback(null, url + ' html content'); }, delay); } async.mapLimit(topicUrls, 5, function(url,callback){ fetchUrl(url, callback); }, function (err, result) { console.log('final:'); console.log(result); }); });
it('after, 0 time', function () { var obj = new EventProxy(); var counter = 0; obj.after('event', 0, function (data) { assert.deepEqual(data.join(","), "", 'Return array should be []'); counter += 1; }); assert.deepEqual(counter, 1, 'counter should be incremented.'); });
router.get('/group', function (req, res) { var ep = new EventProxy(); ep.after('got', 10, function (list) { res.json(list); }); var _ = require('lodash'); _.range(10).forEach(x => fs.readFile(x, ep.group('got'))); });
superagent.get(cnodeUrl).end(function(err,sres) { if(err) { console.log(err); } var topicUrls =[]; var $ = cheerio.load(sres.text); $('#topic_list .topic_title').each(function(index,item) { var $item = $(item); var topicUrl = url.resolve(cnodeUrl,$item.attr('href')); topicUrls.push(topicUrl); }); ep.after('top_html',topicUrls.length,function(topics) { topics=topics.map( function (topicPair) { var topUrl=topicPair[0]; var topHtml = topicPair[1]; var $ = cheerio.load(topHtml); return ({ title: $('.topic_full_title').text().trim(), href: topUrl, comment1: $('.reply_content').eq(0).text().trim(), }); } ); console.log('final:'); console.log(topics); }); topicUrls.forEach(function(topicUrl) { superagent.get(topicUrl).end(function(err,sres) { console.log('fetch '+topicUrl+' successful!'); ep.emit('top_html',[topicUrl,sres.text]); }); }); console.log(topicUrls); });
router.get('/after', function (req, res) { var ep = new EventProxy(); ep.after('got', 10, function (list) { res.json(list); }); var _ = require('lodash'); });
Message.getMessagesByUserId(user_id, function (err, docs) { /*判断是否出错*/ if (err) { return next(err); } for (var i = 0; i < docs.length; i++) { message_ids.push(docs[i]._id); } /*判断是否出错*/ var messages = []; /*如果没有信息*/ if (message_ids.length === 0) { res.render('message/index', {has_read_messages: [], hasnot_read_messages: []}); return; } var proxy = new EventProxy(); var render = function () { var has_read_messages = []; var hasnot_read_messages = []; for (var i = 0; i < messages.length; i++) { if (messages[i].is_invalid) { messages[i].remove(); } else { if (messages[i].has_read) { has_read_messages.push(messages[i]); } else { hasnot_read_messages.push(messages[i]); } } } res.render('message/index', {has_read_messages: has_read_messages, hasnot_read_messages: hasnot_read_messages, }); return; }; //这边是调用上面的方法,下面是对message的遍历 proxy.after('message_ready', message_ids.length, render); message_ids.forEach(function (id, i) { Message.getMessageById(id, function (err, message) { if (err) { return next(err); } messages[i] = message; proxy.emit('message_ready'); }); }); });
]}, function (err, replies_model) { if (err) { return cb(err); } var replies = []; replies_model.map(function(model, key) { replies.push(model.toObject()); }); if (replies.length === 0) { return cb(null, []); } var proxy = new EventProxy(); var done = function () { for (var i = replies.length - 1; i >= 0; i--) { if (replies[i].is_reply) { var srcReply = searchReply(replies[i].reply_id.toString()); replies[i].reply_author = srcReply.author; } } return cb(err, replies); function searchReply(id) { for (var i = replies.length - 1; i >= 0; i--) { if (replies[i].reply_id && replies[i].reply_id.toString() == id) { return replies[i]; } } } }; proxy.after('reply_find', replies.length, done); for (var j = 0; j < replies.length; j++) { (function (i) { var author_id = replies[i].author_id; User.getUserById(author_id, function (err, author) { if (err) { return cb(err); } var avatar_url = author.avatar_url; replies[i].author = author ? author.toObject() : { _id: '' }; replies[i].author.avatar_url = avatar_url; replies[i].friendly_create_at = Util.format_date(replies[i].create_at, true); if (replies[i].content_is_html) { return proxy.emit('reply_find'); } at.linkUsers(replies[i].content, function (err, str) { if (err) { return cb(err); } replies[i].content = str; proxy.emit('reply_find'); }); }); })(j); } });
[have_read, not_read].forEach(function (msgs, idx) { var epfill = new EventProxy(); epfill.fail(next); epfill.after('message_ready', msgs.length, function (docs) { ep.emit(idx === 0 ? 'have_read_msg' : 'not_read_msg', docs); }); msgs.forEach(function (doc) { MessageDao.getDetailMessageById(doc._id, epfill.group('message_ready')); }); });
ep.on('list', function (list) { ep1.after('post', list.length, function (posts) { return cb(null, posts); }).fail(function (err) { return cb(err); }); list.forEach(function (item) { exports.getPost(item, config.post, ep1.group('post')); }); }).fail(function (err) {
.end(function(err, sres) { if (err) console.log(err); var $ = cheerio.load(sres.text); var items = []; $('.main-image img').each(function(idx, element) { var $element = $(element); var img = '<img src="' + $element.attr('src') + '" />' items.push(img); }); var maxIndex = 0; $('.pagenavi span').each(function(idx, element) { var $element = $(element); if (_.isNumber(parseInt($element.text())) && !_.isNaN(parseInt($element.text()))) { if (parseInt($element.text()) > maxIndex) maxIndex = parseInt($element.text()); } }); var ep = new eventproxy(); ep.after('topic_html', maxIndex, function(list) { console.log(list) var body = '<html>' + '<head>' + '<meta http-equiv="Content-Type" content="text/html; ' + 'charset=UTF-8" />' + '</head>' + '<body>' + list.toString() + '</body>' + '</html>'; response.writeHead(200, { "Content-Type": "text/html" }); response.write(body); response.end(); }); for (i = 1; i <= maxIndex; i++) { var link = 'http://www.mzitu.com/' + request.params.id + '/' + i; superagent .get(link) // 如果有些网站需要验证header里面的"User-Agent",则进行如下设置 .set('User-Agent', 'curl/7.24.0 (x86_64-apple-darwin12.0) libcurl/7.24.0 OpenSSL/0.9.8r zlib/1.2.5') .end(function(err, res) { if (err) console.log(err); var $$ = cheerio.load(res.text); $$('.main-image img').each(function(idx, element) { var $element = $$(element); var img = '<img src="' + $element.attr('src') + '" />' items.push(img); console.log('fetch successful' + img); ep.emit('topic_html', img); }); }); } });
router.get('/group', function (req, res) { var ep = new EventProxy(); ep.after('got', 10, function (list) { res.json(list); }); var _ = require('lodash'); let f = __dirname + "/event.js"; _.range(10).forEach( x => fs.readFile(f, 'utf-8', ep.group('got'))); });
.end(function (err, res) { if (err) { return console.error(err); } var topicUrls = []; var $ = cheerio.load(res.text); // 获取首页的所有连接 $("#topic_list .topic_title").each(function (idx, element) { var $element = $(element); // $element.attr('href') 本来的样子是 /topic/542acd7d5d28233425538b04 // 我们用 url.resolve 来自动推断出完整 url,变成 // https://cnodejs.org/topic/542acd7d5d28233425538b04 的形式 // 具体请看 http://nodejs.org/api/url.html#url_url_resolve_from_to 的示例 var href = url.resolve(cnodeUrl, $element.attr("href")); topicUrls.push(href); }); //console.log(topicUrl); // 得到一个 eventproxy 的实例 var ep = new eventproxy(); // 命令 ep 重复监听 topicUrls.length 次(在这里也就是 40 次) `topic_html` 事件再行动 ep.after("topic_html", topicUrls.length, function (topics) { // topics 是个数组,包含了 40 次 ep.emit('topic_html', pair) 中的那 40 个 pair // 开始行动 topics = topics.map(function (topicPair) { // 接下来就是jquery的用法了 var topicUrl = topicPair[0]; var topicHtml = topicPair[1]; var $ = cheerio.load(topicHtml); return ({ title : $(".topic_full_title").text().trim(), href : topicUrl, comment1: $(".reply_content").eq(0).text().trim() }); }); console.log('final:'); console.log(topics); response.send(topics); }); topicUrls.forEach(function (topicUrl) { superagent.get(topicUrl) .end(function (err, res) { //console.log("fetch" + topicUrl + "successful"); ep.emit("topic_html", [topicUrl, res.text]); }); }); });
ep.all('replies', function(replies) { if (!replies || replies.length === 0) { return callback(null, []); } ep.after('affix', replies.length, function() { return callback(null, replies); }); replies.forEach(function(reply, i) { affix(reply, ep.done('affix')); }); });
topic.save(function (err) { if (err) { return next(err); } var proxy = new EventProxy(); var render = function () { res.redirect('/topic/' + topic._id); }; proxy.assign('tags_removed_done', 'tags_saved_done', render); proxy.fail(next); // 删除topic_tag var tags_removed_done = function () { proxy.emit('tags_removed_done'); }; TopicTag.getTopicTagByTopicId(topic._id, function (err, docs) { if (docs.length === 0) { proxy.emit('tags_removed_done'); } else { proxy.after('tag_removed', docs.length, tags_removed_done); // delete topic tags docs.forEach(function (doc) { doc.remove(proxy.done(function () { Tag.getTagById(doc.tag_id, proxy.done(function (tag) { proxy.emit('tag_removed'); tag.topic_count -= 1; tag.save(); })); })); }); } }); // 保存topic_tag var tags_saved_done = function () { proxy.emit('tags_saved_done'); }; //话题可以没有标签 if (topic_tags.length === 0) { proxy.emit('tags_saved_done'); } else { proxy.after('tag_saved', topic_tags.length, tags_saved_done); //save topic tags topic_tags.forEach(function (tag) { TopicTag.newAndSave(topic._id, tag, proxy.done('tag_saved')); Tag.getTagById(tag, proxy.done(function (tag) { tag.topic_count += 1; tag.save(); })); }); } //发送at消息 at.sendMessageToMentionUsers(content, topic._id, req.session.user._id); });
]}, function (err, replies) { if (err) { return cb(err); } if (replies.length === 0) { return cb(null, []); } var proxy = new EventProxy(); var done = function () { var replies2 = []; for (var i = replies.length - 1; i >= 0; i--) { if (replies[i].reply_id) { replies2.push(replies[i]); replies.splice(i, 1); } } for (var j = 0; j < replies.length; j++) { replies[j].replies = []; for (var k = 0; k < replies2.length; k++) { var id1 = replies[j]._id; var id2 = replies2[k].reply_id; if (id1.toString() === id2.toString()) { replies[j].replies.push(replies2[k]); } } replies[j].replies.reverse(); } return cb(err, replies); }; proxy.after('reply_find', replies.length, done); for (var j = 0; j < replies.length; j++) { (function (i) { var author_id = replies[i].author_id; User.getUserById(author_id, function (err, author) { if (err) { return cb(err); } replies[i].author = author || { _id: '' }; replies[i].friendly_create_at = Util.format_date(replies[i].create_at, true); if (replies[i].content_is_html) { return proxy.emit('reply_find'); } at.linkUsers(replies[i].content, function (err, str) { if (err) { return cb(err); } replies[i].content = str; proxy.emit('reply_find'); }); }); })(j); } });
.end(function (err,res) { if(err){ return console.error(err); } var topicUrls = []; var $ = cheerio.load(res.text); $('#topic_list .topic_title').each(function (index,element) { var $element = $(element); var href = url.resolve(codeUrl,$element.attr('href')); topicUrls.push(href); }); // console.log(topicUrls); ep.after('topic_html',topicUrls.length,function(topics){ topics = topics.map(function (topicPair) { var topicUrl = topicPair[0]; var topicHtml = topicPair[1]; var $ = cheerio.load(topicHtml); var title = $('.topic_full_title').text().trim(); fs.appendFile('topicOutput.txt',title+"\r\n",function(err ){ if(err) throw err; console.log("saved."); }); return ({ title: $('.topic_full_title').text().trim(), href: topicUrl, comment1: $('.reply_content').eq(0).text().trim(), }); }); console.log("fianl:"); console.log(topics); }); topicUrls.forEach(function (topicUrl) { superagent.get(topicUrl) .end(function (err,res) { console.log('fetch' + topicUrl + 'successful'); ep.emit('topic_html',[topicUrl, res.text]); }); }); });
.end(function (err, res) { if (err) throw err; var topicUrls = []; var $ = cheerio.load(res.text); $('.box > a').each(function (i, element) { var href = meizhiUrl + $(this).attr('href'); topicUrls.push(href); }); var ep = new eventproxy(); ep.after('topic_html', topicUrls.length, function (topics) { var imgUrls = []; topics.forEach(function (topic) { var $ = cheerio.load(topic); $('.show-box img').each(function (i, element) { var src = $(this).attr('src'); imgUrls.push(src); }); }); imgUrls.forEach(function (imgUrl) { http.get(imgUrl, function (res) { res.setEncoding('binary'); var imageData = ''; res.on('data', function (data) { imageData += data; }).on('end', function () { var imageName = res.headers['etag']; var imageType = res.headers['content-type']; var imageFullName = imageName.split('"')[1] + '.' + imageType.split('/')[1]; fs.writeFile('images/' + imageFullName, imageData, 'binary', function (err) { if (err) throw err; console.log('Image ' + imageFullName +' saved!'); }); }); }); }); }); topicUrls.forEach(function (topicUrl) { superagent.get(topicUrl) .set('User-Agent', 'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:34.0) Gecko/20100101 Firefox/34.0') .end(function (err, res) { if (err) throw err; if (res.statusCode == 200) { console.log('Fetch ' + topicUrl + ' successful!'); ep.emit('topic_html', res.text); } else { console.log('Fetch ' + topicUrl + ' failed!'); } }); }); });
it('get rows from table', function (done) { var rows = [ '02d7MDAwMDAwMDAwMDAwMDAwMw==a', '24e3MDAwMDAwMDAwMDAwMDAwNA==b', '58c8MDAwMDAwMDAwMDAwMDAwMQ==c', 'a98eMDAwMDAwMDAwMDAwMDAwMg==d', '02d7MDAwMDAwMDAwMDAwMDAwMw==a', '24e3MDAwMDAwMDAwMDAwMDAwNA==b', '58c8MDAwMDAwMDAwMDAwMDAwMQ==c', 'a98eMDAwMDAwMDAwMDAwMDAwMg==d', '02d7MDAwMDAwMDAwMDAwMDAwMw==a', '24e3MDAwMDAwMDAwMDAwMDAwNA==b', '58c8MDAwMDAwMDAwMDAwMDAwMQ==c', 'a98eMDAwMDAwMDAwMDAwMDAwMg==d', '02d7MDAwMDAwMDAwMDAwMDAwMw==a', '24e3MDAwMDAwMDAwMDAwMDAwNA==b', '58c8MDAwMDAwMDAwMDAwMDAwMQ==c', 'a98eMDAwMDAwMDAwMDAwMDAwMg==d', '02d7MDAwMDAwMDAwMDAwMDAwMw==a', '24e3MDAwMDAwMDAwMDAwMDAwNA==b', '58c8MDAwMDAwMDAwMDAwMDAwMQ==c', 'a98eMDAwMDAwMDAwMDAwMDAwMg==d' ]; var ep = new EventProxy(); ep.after('put', 4, function () { var count = 10; done = pedding(count, done); var mget = function () { client.mget(tableName, rows, ['f:history', 'f:bigcontent'], function (err, result) { should.not.exists(err); should.exists(result); result.should.length(rows.length); // console.log(result); result.forEach(function (obj) { should.exists(obj); obj.should.have.keys('f:history', 'f:bigcontent'); }); done(); }); }; for (var i = 0; i < count; i++) { mget(); } }); var content = fs.readFileSync(__filename); rows.slice(0, 4).forEach(function (row, i) { client.putRow(tableName, row, { 'f:history': '123' + i, 'f:bigcontent': content }, ep.done('put')); }); });
exports.importTestData = function(items, callback){ var ep = new EventProxy(); var importCount = items.length; var item; ep.after('items', importCount, callback); for (var i = 0; i < importCount; i++) { item = new CategoryAD(); item.category_hash = items[i].category_hash; item.name = items[i].name; item.save(ep.group('items')); } };
exports.delete = function(req, res) { var params = req.parameter; var files = params.fileId; var group = params.groupId; var loginUser = req.loginUser; var ep = new EventProxy(); ep.fail(function(err) { res.json({ err: ERR.SERVER_ERROR, msg: err }); }); ep.after('delete', files.length, function() { res.json({ err: ERR.SUCCESS }); }); var options = { groupId: group && group._id, updateUsed: true }; files.forEach(function(file) { mFile.delete({ _id: file._id }, options, ep.group('delete', function(result) { mLog.create({ fromUser: loginUser, file: file, //操作类型 1: 上传, 2: 下载, 3: copy, 4: move, 5: modify //6: delete 7: 预览 8: 保存, 9: 分享给用户 10: 分享给小组, //11: delete(移动到回收站) 12: 创建文件夹 operateType: 6, srcFolderId: file.folder && file.folder.oid, fromGroupId: file.folder && file.folder.group && file.folder.group.oid }); return result; })); }); };
MessageDao.getMessagesByUserId(uid,function(err,messages){ proxy.after('gotmessages',messages.length,render); messages.forEach(function(message,a){ message.at = Util.format_date(message.create_at, true); UserDao.getUserById(message.author_id,function(err,user){ message.author = user; proxy.emit('gotmessages',message); }); }); });
Cart.findById(cid,function(err,cart){ if(err) console.log(err) if(cart){ // console.log(cart) var ep =new eventproxy() ep.after('differs',cart.differs.length,function(differs){ var did = req.params.did var k = ainb.aatb(did,differs) if(k>0){ cart.differs.splice(k-1,1) if(cart.differs.length>0){ cart.save(function (err, cart) { if (err)console.log(err) // console.log('表示购物车已存在该商品,并且更新数量') User.findByIdAndUpdate({_id:req.session.user._id},{$inc:{cart:-1}},{new: true},function(err,user){ // console.log('-1') // console.log(user.cart) req.session.user=user res.json({success:1}) }) // res.json({success: 1}) //表示购物车已存在该商品 })} else{ // var query = {"_id": cart._id}; // cart = _.extend(cart,Cart) // cart.findByIdAndRemove(query,function(err,cart){ // console.log('success delete') // }) cart.remove({_id:cart._id},function(err,cart){ if(err)console.log(err) User.findByIdAndUpdate({_id:req.session.user._id},{$inc:{cart:-1}},{new: true},function(err,user){ // console.log('-1') // console.log(user.cart) req.session.user=user res.json({success:9}) }) // res.json({success:9})//全删了 }) } }else{ res.json({success:2})//查无此商品 } }) for (var i=0;i<cart.differs.length;i++){ ep.emit('differs',cart.differs[i].differ_id.toString()) } } })
.end(function(err,res){ if(err){ return console.log(err); } var topicUrls = []; var $ = cheerio.load(res.text); //get the index page url $('#topic_list .topic_title').each(function(idx,element){ var $element = $(element); var href = url.resolve(cnodeUrl,$element.attr('href')); topicUrls.push(href); }); console.log(topicUrls); var ep = new eventproxy(); ep.after('topic_html',topicUrls.length,function(topics){ topics = topics.map(function(topicPair){ var topicUrl = topicPair[0]; var topicHtml = topicPair[1]; var $ = cheerio.load(topicHtml); return ({ title:$('.topic_full_title').text().trim(), href:topicUrl, comment1:$('reply_content').eq(0).text().trim() }); }); console.log('final'); console.log(topics); }); topicUrls.forEach(function(topicUrl){ superagent.get(topicUrl) .end(function(err,res){ console.log('fetch'+topicUrl+'successful'); ep.emit('topic_html',[topicUrl,res.text]); }); }); });