Code, Shiny Things, and Other Random Shit I Like

jamesreinknecht.com

Mirror Mirror On The Wall

In my 100 Days of code challenge, my day 3 was spent debugging homework. The example set in our repository had a giant bug in it, causing errors from the promise library and to the browser.

I discovered a bug in the scoping of a forEach loop (yeah yeah i should be using .map, eat me) where at the end of each loop it was pushing to the database and then doing a res.send… the browser was receiving 25 responses and freaking out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
app.get('/scrape', function(req, res) {
axios.get('http://www.echojs.com/').then(function(response) {
var $ = cheerio.load(response.data);
$('article h2').each(function(i, element) {
var result = {};
result.title = $(this).children('a').text();
result.link = $(this).children('a').attr('href');
db.Article.create(result)
.then(function(dbArticle) {
console.log('scrape complete', dbArticle);
res.send('Scrape Complete');
})
.catch(function(err) {
res.json(err);
});
});

});
});

produces:

bugfezt

can you find it?

tl: dr; no one cares, zlz

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
app.get('/scrape', function(req, res) {
axios.get('http://www.echojs.com/').then(function(response) {
var $ = cheerio.load(response.data);
$('article h2').each(function(i, element) {
var result = {};
result.title = $(this).children('a').text();
result.link = $(this).children('a').attr('href');
db.Article.create(result)
.then(function(dbArticle) {
console.log('scrape complete', dbArticle);

})
.catch(function(err) {
res.json(err);
});
});
res.send('Scrape Complete');
});
});

#100DaysOfCode #Day3