From bba9b064dff2636bf11cb624169c041dc94747ba Mon Sep 17 00:00:00 2001 From: Henry Wang Date: Tue, 28 Aug 2018 11:09:06 +0100 Subject: [PATCH] Skip when hopper returns no result (#553) prevent application from throwing an error --- routes/hopper/index.js | 42 ++++++++++++++++++++++-------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/routes/hopper/index.js b/routes/hopper/index.js index 0d466256c9..e4b19711c0 100644 --- a/routes/hopper/index.js +++ b/routes/hopper/index.js @@ -23,29 +23,31 @@ module.exports = async (ctx) => { const list = $('div.prices li a'); const items = []; - if (type === '1') { - let lowest = 99999; - let lowIndex = 0; + if (!list.length === 0) { + if (type === '1') { + let lowest = 99999; + let lowIndex = 0; - list.each((i, e) => { - const current = parseInt( - $(e) - .find('.price') - .text() - .replace(/\D/g, '') - ); + list.each((i, e) => { + const current = parseInt( + $(e) + .find('.price') + .text() + .replace(/\D/g, '') + ); - if (current < lowest) { - lowest = current; - lowIndex = i; - } - }); + if (current < lowest) { + lowest = current; + lowIndex = i; + } + }); - items.push(formatDesc($(list[lowIndex]))); - } else { - list.each((i, e) => { - items.push(formatDesc($(e))); - }); + items.push(formatDesc($(list[lowIndex]))); + } else { + list.each((i, e) => { + items.push(formatDesc($(e))); + }); + } } ctx.state.data = {