Authored by jinhu.tung

catch error in middleware

... ... @@ -9,7 +9,7 @@
const cartModel = require('../models/cart');
const _ = require('lodash');
exports.index = (req, res) => {
exports.index = (req, res, next) => {
const shoppingKey = req.cookies._SPK || null;
const uid = req.user.uid || null;
... ... @@ -28,14 +28,12 @@ exports.index = (req, res) => {
// code 500
res.send(result);
}
}).catch((err) => {
res.send(err);
});
}).catch(next);
};
// 修改数量
exports.changeProductNum = (req, res) => {
exports.changeProductNum = (req, res, next) => {
const changeType = req.body.changeType;
// const changeTo = req.body.changeTo;
... ... @@ -52,7 +50,7 @@ exports.changeProductNum = (req, res) => {
sku,
goodTpye
}).then(result => {
console.log('changeProductNum-INCREASE-result:', result);
//console.log('changeProductNum-INCREASE-result:', result);
if (result.code === 200) {
cartModel.getCartData(shoppingKey, uid).then(cartData => {
res.json(_.merge(
... ... @@ -63,7 +61,7 @@ exports.changeProductNum = (req, res) => {
} else {
res.json(result);
}
});
}).catch(next);
} else if (changeType === 'DECREASE') {
cartModel.modifyProductNum({
... ... @@ -73,7 +71,7 @@ exports.changeProductNum = (req, res) => {
sku,
goodTpye
}).then(result => {
console.log('changeProductNum-DECREASE-result:', JSON.stringify(result));
//console.log('changeProductNum-DECREASE-result:', JSON.stringify(result));
if (result.code === 200) {
cartModel.getCartData(shoppingKey, uid).then(cartData => {
res.json(_.merge(
... ... @@ -84,12 +82,12 @@ exports.changeProductNum = (req, res) => {
} else {
res.json(result);
}
});
}).catch(next);
}
};
// 删除商品
exports.removeProduct = (req, res) => {
exports.removeProduct = (req, res, next) => {
const params = {};
const uid = req.user.uid;
const shoppingKey = req.cookies._SPK;
... ... @@ -107,16 +105,16 @@ exports.removeProduct = (req, res) => {
_.merge(params, {product_sku_list: productSkuList});
}
console.log('remove---params:', params);
//console.log('remove---params:', params);
cartModel.removeGoods(params).then(result => {
console.log('remove-result:', result);
//console.log('remove-result:', result);
res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
});
}).catch(next);
};
// 收藏商品
exports.sendToFavorite = (req, res) => {
exports.sendToFavorite = (req, res, next) => {
const params = {};
const uid = req.user.uid;
const productSkuList = req.body.skuList;
... ... @@ -134,7 +132,7 @@ exports.sendToFavorite = (req, res) => {
cartModel.transferToFavorite(params).then(result => {
console.log('transferToFavorite-result:', result);
res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
});
}).catch(next);
};
// 编辑商品颜色和尺寸
... ... @@ -158,7 +156,7 @@ exports.editProduct = (req, res) => {
};
// 添加商品到购物车
exports.addToCart = (req, res) => {
exports.addToCart = (req, res, next) => {
const productSku = req.body.productSku;
const buyNumber = req.body.buyNumber;
... ... @@ -170,11 +168,11 @@ exports.addToCart = (req, res) => {
}).then((result) => {
res.cookie('_SPK', result.data.shopping_key, { maxAge: 1000 * 60 * 60 * 24 * 365, httpOnly: true, path: '/'});
res.json(result);
});
}).catch(next);
};
// 购物车商品选择与取消选择
exports.toggleSelectGoods = (req, res) => {
exports.toggleSelectGoods = (req, res, next) => {
const params = {};
const uid = req.user.uid;
const shoppingKey = req.cookies._SPK;
... ... @@ -201,23 +199,23 @@ exports.toggleSelectGoods = (req, res) => {
cartModel.toggleSelectGoods(params).then(result => {
res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
});
}).catch(next);
};
// 去结算检查库存
exports.checkStorage = (req, res) => {
exports.checkStorage = (req, res, next) => {
const shoppingKey = req.cookies._SPK || null;
const uid = req.user.uid || null;
cartModel.getCartData(shoppingKey, uid).then((result) => {
// console.log('Cart current Data:', JSON.stringify(result, '', 4))
res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
});
}).catch(next);
};
// 修改购物车商品颜色和尺寸
exports.modifyProduct = (req, res) => {
exports.modifyProduct = (req, res, next) => {
const uid = req.user && req.user.uid;
const shoppingKey = req.cookies._SPK;
... ... @@ -238,7 +236,7 @@ exports.modifyProduct = (req, res) => {
} else {
res.json(result);
}
});
}).catch(next);
};
... ...