You should check res before accessing res.error