Authored by jinhu.tung

catch error in middleware

@@ -9,7 +9,7 @@ @@ -9,7 +9,7 @@
9 const cartModel = require('../models/cart'); 9 const cartModel = require('../models/cart');
10 const _ = require('lodash'); 10 const _ = require('lodash');
11 11
12 -exports.index = (req, res) => { 12 +exports.index = (req, res, next) => {
13 const shoppingKey = req.cookies._SPK || null; 13 const shoppingKey = req.cookies._SPK || null;
14 const uid = req.user.uid || null; 14 const uid = req.user.uid || null;
15 15
@@ -28,14 +28,12 @@ exports.index = (req, res) => { @@ -28,14 +28,12 @@ exports.index = (req, res) => {
28 // code 500 28 // code 500
29 res.send(result); 29 res.send(result);
30 } 30 }
31 - }).catch((err) => {  
32 - res.send(err);  
33 - }); 31 + }).catch(next);
34 }; 32 };
35 33
36 34
37 // 修改数量 35 // 修改数量
38 -exports.changeProductNum = (req, res) => { 36 +exports.changeProductNum = (req, res, next) => {
39 const changeType = req.body.changeType; 37 const changeType = req.body.changeType;
40 38
41 // const changeTo = req.body.changeTo; 39 // const changeTo = req.body.changeTo;
@@ -52,7 +50,7 @@ exports.changeProductNum = (req, res) => { @@ -52,7 +50,7 @@ exports.changeProductNum = (req, res) => {
52 sku, 50 sku,
53 goodTpye 51 goodTpye
54 }).then(result => { 52 }).then(result => {
55 - console.log('changeProductNum-INCREASE-result:', result); 53 + //console.log('changeProductNum-INCREASE-result:', result);
56 if (result.code === 200) { 54 if (result.code === 200) {
57 cartModel.getCartData(shoppingKey, uid).then(cartData => { 55 cartModel.getCartData(shoppingKey, uid).then(cartData => {
58 res.json(_.merge( 56 res.json(_.merge(
@@ -63,7 +61,7 @@ exports.changeProductNum = (req, res) => { @@ -63,7 +61,7 @@ exports.changeProductNum = (req, res) => {
63 } else { 61 } else {
64 res.json(result); 62 res.json(result);
65 } 63 }
66 - }); 64 + }).catch(next);
67 65
68 } else if (changeType === 'DECREASE') { 66 } else if (changeType === 'DECREASE') {
69 cartModel.modifyProductNum({ 67 cartModel.modifyProductNum({
@@ -73,7 +71,7 @@ exports.changeProductNum = (req, res) => { @@ -73,7 +71,7 @@ exports.changeProductNum = (req, res) => {
73 sku, 71 sku,
74 goodTpye 72 goodTpye
75 }).then(result => { 73 }).then(result => {
76 - console.log('changeProductNum-DECREASE-result:', JSON.stringify(result)); 74 + //console.log('changeProductNum-DECREASE-result:', JSON.stringify(result));
77 if (result.code === 200) { 75 if (result.code === 200) {
78 cartModel.getCartData(shoppingKey, uid).then(cartData => { 76 cartModel.getCartData(shoppingKey, uid).then(cartData => {
79 res.json(_.merge( 77 res.json(_.merge(
@@ -84,12 +82,12 @@ exports.changeProductNum = (req, res) => { @@ -84,12 +82,12 @@ exports.changeProductNum = (req, res) => {
84 } else { 82 } else {
85 res.json(result); 83 res.json(result);
86 } 84 }
87 - }); 85 + }).catch(next);
88 } 86 }
89 }; 87 };
90 88
91 // 删除商品 89 // 删除商品
92 -exports.removeProduct = (req, res) => { 90 +exports.removeProduct = (req, res, next) => {
93 const params = {}; 91 const params = {};
94 const uid = req.user.uid; 92 const uid = req.user.uid;
95 const shoppingKey = req.cookies._SPK; 93 const shoppingKey = req.cookies._SPK;
@@ -107,16 +105,16 @@ exports.removeProduct = (req, res) => { @@ -107,16 +105,16 @@ exports.removeProduct = (req, res) => {
107 _.merge(params, {product_sku_list: productSkuList}); 105 _.merge(params, {product_sku_list: productSkuList});
108 } 106 }
109 107
110 - console.log('remove---params:', params); 108 + //console.log('remove---params:', params);
111 109
112 cartModel.removeGoods(params).then(result => { 110 cartModel.removeGoods(params).then(result => {
113 - console.log('remove-result:', result); 111 + //console.log('remove-result:', result);
114 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message})); 112 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
115 - }); 113 + }).catch(next);
116 }; 114 };
117 115
118 // 收藏商品 116 // 收藏商品
119 -exports.sendToFavorite = (req, res) => { 117 +exports.sendToFavorite = (req, res, next) => {
120 const params = {}; 118 const params = {};
121 const uid = req.user.uid; 119 const uid = req.user.uid;
122 const productSkuList = req.body.skuList; 120 const productSkuList = req.body.skuList;
@@ -134,7 +132,7 @@ exports.sendToFavorite = (req, res) => { @@ -134,7 +132,7 @@ exports.sendToFavorite = (req, res) => {
134 cartModel.transferToFavorite(params).then(result => { 132 cartModel.transferToFavorite(params).then(result => {
135 console.log('transferToFavorite-result:', result); 133 console.log('transferToFavorite-result:', result);
136 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message})); 134 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
137 - }); 135 + }).catch(next);
138 }; 136 };
139 137
140 // 编辑商品颜色和尺寸 138 // 编辑商品颜色和尺寸
@@ -158,7 +156,7 @@ exports.editProduct = (req, res) => { @@ -158,7 +156,7 @@ exports.editProduct = (req, res) => {
158 }; 156 };
159 157
160 // 添加商品到购物车 158 // 添加商品到购物车
161 -exports.addToCart = (req, res) => { 159 +exports.addToCart = (req, res, next) => {
162 const productSku = req.body.productSku; 160 const productSku = req.body.productSku;
163 const buyNumber = req.body.buyNumber; 161 const buyNumber = req.body.buyNumber;
164 162
@@ -170,11 +168,11 @@ exports.addToCart = (req, res) => { @@ -170,11 +168,11 @@ exports.addToCart = (req, res) => {
170 }).then((result) => { 168 }).then((result) => {
171 res.cookie('_SPK', result.data.shopping_key, { maxAge: 1000 * 60 * 60 * 24 * 365, httpOnly: true, path: '/'}); 169 res.cookie('_SPK', result.data.shopping_key, { maxAge: 1000 * 60 * 60 * 24 * 365, httpOnly: true, path: '/'});
172 res.json(result); 170 res.json(result);
173 - }); 171 + }).catch(next);
174 }; 172 };
175 173
176 // 购物车商品选择与取消选择 174 // 购物车商品选择与取消选择
177 -exports.toggleSelectGoods = (req, res) => { 175 +exports.toggleSelectGoods = (req, res, next) => {
178 const params = {}; 176 const params = {};
179 const uid = req.user.uid; 177 const uid = req.user.uid;
180 const shoppingKey = req.cookies._SPK; 178 const shoppingKey = req.cookies._SPK;
@@ -201,23 +199,23 @@ exports.toggleSelectGoods = (req, res) => { @@ -201,23 +199,23 @@ exports.toggleSelectGoods = (req, res) => {
201 199
202 cartModel.toggleSelectGoods(params).then(result => { 200 cartModel.toggleSelectGoods(params).then(result => {
203 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message})); 201 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
204 - }); 202 + }).catch(next);
205 }; 203 };
206 204
207 205
208 // 去结算检查库存 206 // 去结算检查库存
209 -exports.checkStorage = (req, res) => { 207 +exports.checkStorage = (req, res, next) => {
210 const shoppingKey = req.cookies._SPK || null; 208 const shoppingKey = req.cookies._SPK || null;
211 const uid = req.user.uid || null; 209 const uid = req.user.uid || null;
212 210
213 cartModel.getCartData(shoppingKey, uid).then((result) => { 211 cartModel.getCartData(shoppingKey, uid).then((result) => {
214 // console.log('Cart current Data:', JSON.stringify(result, '', 4)) 212 // console.log('Cart current Data:', JSON.stringify(result, '', 4))
215 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message})); 213 res.json(_.merge(cartModel.filterCartData(result, uid), {code: result.code, message: result.message}));
216 - }); 214 + }).catch(next);
217 }; 215 };
218 216
219 // 修改购物车商品颜色和尺寸 217 // 修改购物车商品颜色和尺寸
220 -exports.modifyProduct = (req, res) => { 218 +exports.modifyProduct = (req, res, next) => {
221 const uid = req.user && req.user.uid; 219 const uid = req.user && req.user.uid;
222 const shoppingKey = req.cookies._SPK; 220 const shoppingKey = req.cookies._SPK;
223 221
@@ -238,7 +236,7 @@ exports.modifyProduct = (req, res) => { @@ -238,7 +236,7 @@ exports.modifyProduct = (req, res) => {
238 } else { 236 } else {
239 res.json(result); 237 res.json(result);
240 } 238 }
241 - }); 239 + }).catch(next);
242 }; 240 };
243 241
244 242