Mohamed Abu Basith commited on
Commit
097fefe
·
1 Parent(s): d1794f9
Files changed (1) hide show
  1. routes/profileUpdate.js +17 -3
routes/profileUpdate.js CHANGED
@@ -194,10 +194,13 @@ router.get("/:userId/addresses", asyncHandler(async (req, res) => {
194
  // Update address
195
  router.put("/:userId/addresses/:addressId", asyncHandler(async (req, res) => {
196
  try {
 
 
 
197
  const updatedUser = await User.findOneAndUpdate(
198
  {
199
  unique_id: req.params.userId,
200
- "addresses._id": req.params.addressId // Compare as string
201
  },
202
  {
203
  $set: {
@@ -211,14 +214,25 @@ router.put("/:userId/addresses/:addressId", asyncHandler(async (req, res) => {
211
  return res.status(404).json(failedResponse("User or address not found", 404));
212
  }
213
 
 
 
 
 
 
 
 
 
 
 
 
214
  // Find the updated address by comparing _id as a string
215
- const updatedAddress = updatedUser.addresses.find(a => a._id === req.params.addressId);
216
 
217
  if (!updatedAddress) {
 
218
  return res.status(404).json(failedResponse("Address not found in the user's addresses", 404));
219
  }
220
 
221
- console.log("Updated User:", updatedUser);
222
  console.log("Updated Address:", updatedAddress);
223
 
224
  res.status(200).json(successResponse("Address updated successfully", updatedAddress));
 
194
  // Update address
195
  router.put("/:userId/addresses/:addressId", asyncHandler(async (req, res) => {
196
  try {
197
+ const addressId = req.params.addressId.trim(); // Trim whitespace
198
+ console.log("Request Address ID:", addressId);
199
+
200
  const updatedUser = await User.findOneAndUpdate(
201
  {
202
  unique_id: req.params.userId,
203
+ "addresses._id": addressId // Compare as string
204
  },
205
  {
206
  $set: {
 
214
  return res.status(404).json(failedResponse("User or address not found", 404));
215
  }
216
 
217
+ console.log("Updated User:", updatedUser);
218
+
219
+ // Debug: Log all addresses and their _id values
220
+ updatedUser.addresses.forEach((a, index) => {
221
+ console.log(`Address ${index}:`, {
222
+ _id: a._id,
223
+ addressId: addressId,
224
+ match: a._id === addressId
225
+ });
226
+ });
227
+
228
  // Find the updated address by comparing _id as a string
229
+ const updatedAddress = updatedUser.addresses.find(a => a._id.toString().trim() === addressId.toString().trim());
230
 
231
  if (!updatedAddress) {
232
+ console.log("Addresses Array:", updatedUser.addresses); // Log the addresses array
233
  return res.status(404).json(failedResponse("Address not found in the user's addresses", 404));
234
  }
235
 
 
236
  console.log("Updated Address:", updatedAddress);
237
 
238
  res.status(200).json(successResponse("Address updated successfully", updatedAddress));