Mohamed Abu Basith commited on
Commit
5bdae11
·
1 Parent(s): 654946f
Files changed (1) hide show
  1. routes/profileUpdate.js +13 -1
routes/profileUpdate.js CHANGED
@@ -95,6 +95,7 @@ const asyncHandler = fn => (req, res, next) =>
95
  // Create address (add to user's address array)
96
  router.post("/:userId/addresses", asyncHandler(async (req, res) => {
97
  const userId = parseInt(req.params.userId);
 
98
  if (isNaN(userId)) {
99
  return res.status(400).json(failedResponse("Invalid user ID", 400));
100
  }
@@ -109,10 +110,21 @@ router.post("/:userId/addresses", asyncHandler(async (req, res) => {
109
  return res.status(404).json(failedResponse("User not found", 404));
110
  }
111
 
112
- // Add safety check for address array
 
 
 
 
113
  const addressArray = updatedUser.address || [];
114
  const newAddress = addressArray[addressArray.length - 1];
115
 
 
 
 
 
 
 
 
116
  res.status(201).json(successResponse("Address created successfully", newAddress));
117
  }));
118
 
 
95
  // Create address (add to user's address array)
96
  router.post("/:userId/addresses", asyncHandler(async (req, res) => {
97
  const userId = parseInt(req.params.userId);
98
+ console.log(" -----> " + userId)
99
  if (isNaN(userId)) {
100
  return res.status(400).json(failedResponse("Invalid user ID", 400));
101
  }
 
110
  return res.status(404).json(failedResponse("User not found", 404));
111
  }
112
 
113
+ // Ensure the address array exists
114
+ if (!updatedUser.address || !Array.isArray(updatedUser.address)) {
115
+ return res.status(500).json(failedResponse("User address field is invalid", 500));
116
+ }
117
+
118
  const addressArray = updatedUser.address || [];
119
  const newAddress = addressArray[addressArray.length - 1];
120
 
121
+ console.log("Updated User:", updatedUser);
122
+ console.log("New Address:", newAddress);
123
+
124
+ if (!newAddress) {
125
+ return res.status(500).json(failedResponse("Failed to retrieve new address", 500));
126
+ }
127
+
128
  res.status(201).json(successResponse("Address created successfully", newAddress));
129
  }));
130