- Created `/frontend/app/api/votes/check/route.ts` to handle GET requests for checking if a user has voted in a specific election. - Added error handling for unauthorized access and missing election ID. - Forwarded requests to the backend API and returned appropriate responses. - Updated `/frontend/app/api/votes/history/route.ts` to fetch user's voting history with error handling. - Ensured both endpoints utilize the authorization token for secure access.
2.2 KiB
2.2 KiB
Quick Fix Summary - Blockchain Dashboard
🐛 The Problems
1. Console Error: truncateHash: invalid hash parameter: undefined
- What: Random hash fields showing as undefined
- Why: Genesis block and empty vote fields weren't validated
- Fix: Added null/undefined checks before truncating
2. POST Error: {"detail":[{"type":"missing","loc":["query","election_id"]...
- What: Verification button fails with "Field required"
- Why: Frontend sends
election_idin body, backend expects it in query string - How it failed:
Frontend: POST /api/votes/verify-blockchain { body: {election_id: 1} } ↓ NextJS Proxy: Ignored the body, forgot to add election_id to URL ↓ Backend: POST /api/votes/verify-blockchain? ← No election_id! ↓ Error: "election_id is required"
✅ What Was Fixed
File 1: /frontend/app/api/votes/verify-blockchain/route.ts
+ const body = await request.json()
+ if (body.election_id) {
+ url.searchParams.append('election_id', body.election_id.toString())
+ }
Result: election_id now passed as query parameter to backend
File 2: /frontend/components/blockchain-viewer.tsx
const truncateHash = (hash: string, length: number = 16) => {
+ if (!hash || typeof hash !== "string") {
+ return "N/A"
+ }
return hash.length > length ? `${hash.slice(0, length)}...` : hash
}
Result: Graceful handling of undefined/empty hash values
🧪 Test It
- Load dashboard: http://localhost:3000/dashboard/blockchain
- Select an election → should load without errors
- Click verify button → should show verification result
- Check console → should have no truncateHash errors
🎯 Root Cause Analysis
| Issue | Root Cause | Solution |
|---|---|---|
| truncateHash errors | No type validation on hash parameter | Add null/undefined checks |
| Missing election_id | NextJS proxy didn't forward body to backend | Parse body and add to query params |
| Field required error | Backend expects query param, frontend sends body | Convert body param to query param in proxy |
📚 Documentation
See BLOCKCHAIN_DASHBOARD_FIX.md for detailed analysis and testing procedures.