Error handling for queries.

This commit is contained in:
Mike Cao 2020-08-31 21:11:53 -07:00
parent cebd3fd924
commit cb48acde55
3 changed files with 11 additions and 8 deletions

View file

@ -3,11 +3,14 @@ import { ok } from 'lib/response';
export default async (req, res) => {
const { id, start_at, end_at } = req.query;
const websiteId = +id;
const startDate = new Date(+start_at);
const endDate = new Date(+end_at);
const metrics = await getMetrics(+id, new Date(+start_at), new Date(+end_at));
const metrics = await getMetrics(websiteId, startDate, endDate);
const stats = Object.keys(metrics[0]).reduce((obj, key) => {
obj[key] = +metrics[0][key];
obj[key] = Number(metrics[0][key]) || 0;
return obj;
}, {});