Skip to content

Commit

Permalink
adds Promise catches to profileImage
Browse files Browse the repository at this point in the history
  • Loading branch information
davelandry committed Apr 4, 2019
1 parent 51ee889 commit 91c22ed
Showing 1 changed file with 21 additions and 3 deletions.
24 changes: 21 additions & 3 deletions api/profileImage.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,12 @@ module.exports = function(app) {

const id = image ? image : pslug === "university" ? "2032" : "1849";
if (size === "json") db.images.findOne({where: {id}}).then(resp => res.json(resp));
else res.sendFile(`${process.cwd()}/static/images/profile/${size}/${id}.jpg`);
else {
res.sendFile(`${process.cwd()}/static/images/profile/${size}/${id}.jpg`, err => {
if (err) res.status(err.status);
res.end();
});
}

}

Expand All @@ -51,6 +56,10 @@ module.exports = function(app) {
.sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id))
.find(p => p.imageId);
sendImage(parentImage ? parentImage.imageId : false);
})
.catch(err => {
console.error(`[api/profileImage] parent cache error for ${pslug}/${pid}: (${err.status ? `${err.status} - ` : ""}${err.message})}`);
sendImage(false);
});

}
Expand All @@ -65,25 +74,34 @@ module.exports = function(app) {
.then(d => d.data.reverse())
.then(d => d.map(p => p.geoid))
.then(d => {
const attrs = db.search.findAll({where: {id: d, dimension: slugMap[pslug]}});
const attrs = db.search.findAll({where: {id: d, dimension: slugMap[pslug]}}).catch(() => []);
return Promise.all([d, attrs]);
})
.then(([ids, parentAttrs]) => {
const parentImage = parentAttrs
.sort((a, b) => ids.indexOf(a.id) - ids.indexOf(b.id))
.find(p => p.imageId).imageId;
sendImage(parentImage);
})
.catch(err => {
console.error(`[api/profileImage] geoservice error for ${pslug}/${pid}: (${err.status ? `${err.status} - ` : ""}${err.message})}`);
sendImage(false);
});

}
else sendImage();
else sendImage(false);

}
else sendImage(imageId);

}

})
.catch(err => {
console.error(`[api/profileImage] search matching for ${pslug}/${pid}: (${err.status ? `${err.status} - ` : ""}${err.message})}`);
sendImage(false);
});

});

};

0 comments on commit 91c22ed

Please sign in to comment.