Error deleting batch: SyntaxError: Failed to execute 'json' on 'Response':
Unexpected end of JSON input at handleDeleteConfirm
- Backend tidak mengembalikan JSON response
- Response body kosong
- Field name mismatch (
okvscanDelete) - Server error sebelum return statement
// OLD (Error prone):
const data = await response.json();
// NEW (Safe):
const contentType = response.headers.get("content-type");
if (contentType && contentType.includes("application/json")) {
const text = await response.text();
if (text) {
data = JSON.parse(text);
}
}// OLD:
return { ok: true, blockedProductIds: [] };
// NEW:
return { canDelete: true, blockedProductIds: [] };// Always use res.status().json()
return res.status(200).json({
success: true,
message: "...",
deletedProducts: false,
});Symptom:
{
"error": "Internal server error",
"timestamp": "..."
}Causes:
- Database connection error
- Prisma query error
- Undefined variable
Solution:
- Check server logs
- Check database connection
- Verify batch ID exists
- Check Prisma schema
Test:
# Check if batch exists
curl http://localhost:3001/api/bulk-upload/{batchId}
# Check database
node server/tests/test-db-bulk.jsSymptom:
{
"error": "Cannot delete products: Some products are in orders",
"timestamp": "..."
}Cause: Protection working correctly! Products can't be deleted if they're in orders.
Solution:
-
Option A: Delete batch only (keep products)
- Uncheck "Also delete all products" checkbox
- Products will remain in catalog
-
Option B: Delete orders first (NOT RECOMMENDED)
DELETE FROM customer_order_product WHERE productId IN (SELECT productId FROM bulk_upload_item WHERE batchId = ?);
-
Option C: Keep batch and products
- Don't delete, just keep for history
Test Which Products Are Blocking:
curl http://localhost:3001/api/bulk-upload/{batchId}
# Check which products have productIdSymptom:
- Click delete button
- Modal shows
- Click confirm
- Nothing happens
- No error, no success message
Causes:
- Frontend not connected to backend
- CORS error
- Server not running
- Wrong API URL
Solution:
# 1. Check if server is running
curl http://localhost:3001/api/bulk-upload
# 2. Check CORS
# Open browser DevTools > Network tab
# Look for CORS errors
# 3. Check frontend API URL
# File: components/BulkUploadHistory.tsx
# Should be: http://localhost:3001/api/bulk-upload/{id}Symptom:
- Delete with "Also delete products" checked
- Success message shown
- But products still in catalog
Cause: Working as designed! If products are in orders, they won't be deleted.
Verify:
# Check products
curl http://localhost:3001/api/products
# Check if products in orders
# (Products in orders are protected)Symptom:
Response Body: (empty)
Causes:
- Server crashed before sending response
- Error in transaction
- Database locked
Solution:
-
Check server logs:
Get-Content server/logs/error.log | Select-Object -Last 50 -
Restart server:
cd server npm start -
Test with curl:
curl -X DELETE "http://localhost:3001/api/bulk-upload/{id}?deleteProducts=false" -v
-
Server is running
curl http://localhost:3001/api/bulk-upload # Should return JSON with batches -
At least one batch exists
# Upload test batch curl -X POST http://localhost:3001/api/bulk-upload -F "file=@bulk-upload-example.csv"
-
Frontend is running
# Should be at http://localhost:3000 -
Database is accessible
node server/tests/test-db-bulk.js
# 1. Get batch ID
curl http://localhost:3001/api/bulk-upload
# 2. Delete batch (keep products)
curl -X DELETE "http://localhost:3001/api/bulk-upload/{BATCH_ID}?deleteProducts=false"
# Expected response:
# {
# "success": true,
# "message": "Batch deleted successfully (products kept)",
# "deletedProducts": false
# }
# 3. Verify batch gone
curl http://localhost:3001/api/bulk-upload
# 4. Verify products still exist
curl http://localhost:3001/api/products# 1. Upload fresh batch
curl -X POST http://localhost:3001/api/bulk-upload -F "file=@bulk-upload-example.csv"
# Save batchId
# 2. Delete batch + products
curl -X DELETE "http://localhost:3001/api/bulk-upload/{BATCH_ID}?deleteProducts=true"
# Expected response:
# {
# "success": true,
# "message": "Batch and products deleted successfully",
# "deletedProducts": true
# }
# 3. Verify products deleted
curl http://localhost:3001/api/products
# Products from batch should be gone# Windows
Get-Process -Name node
# Check port 3001
netstat -ano | findstr :3001cd server
node tests/test-db-bulk.jscurl http://localhost:3001/api/bulk-upload/{batchId}-- Delete batch only
DELETE FROM bulk_upload_item WHERE batchId = 'xxx';
DELETE FROM bulk_upload_batch WHERE id = 'xxx';
-- Delete batch + products
DELETE FROM Product WHERE id IN (
SELECT productId FROM bulk_upload_item WHERE batchId = 'xxx'
);
DELETE FROM bulk_upload_item WHERE batchId = 'xxx';
DELETE FROM bulk_upload_batch WHERE id = 'xxx';- Always check server logs before reporting issues
- Test with curl first before using UI
- Verify batch ID is correct
- Check if products are in orders before trying to delete
- Use "Delete Batch Only" if unsure
- Don't force delete products in orders
- Don't delete batches without backup
- Don't spam delete button (rate limiter)
- Don't modify database directly without backup
components/BulkUploadHistory.tsx- Added safe JSON parsing
- Better error handling
- Loading states
-
server/services/bulkUploadService.js- Fixed
canDeleteProductsForBatchreturn format - Changed
oktocanDelete - Added
reasonfield
- Fixed
-
server/controllers/bulkUpload.js- Always return JSON
- Better logging
- Consistent response format
Problem: JSON parse error on delete
Root Cause: Field mismatch (ok vs canDelete)
Fix:
- ✅ Updated
canDeleteProductsForBatchto returncanDelete - ✅ Added safe JSON parsing in frontend
- ✅ Better error messages
- ✅ Consistent response format
Test:
node server/tests/test-delete-batch.js🎉 Issue Resolved!