4.0 KiB
Notification Templates Error Debugging Guide
Issue Description
Error: Cannot read properties of undefined (reading 'map')
Location: pages/notification/templates/index.vue line 399
Cause: The frontend is trying to call .map() on response.data.templates but this property is undefined.
Root Cause Analysis
The error occurs because the API response doesn't have the expected structure. The frontend expects:
{
success: true,
data: {
templates: [...], // Array of templates
pagination: {...}
}
}
But the API might be returning:
- An error response
- A response with missing
dataproperty - A response with missing
data.templatesproperty - An empty or malformed response
Potential Causes
-
Database Issues:
- Database connection failure
- Missing
notification_templatestable - Table exists but has no data
-
Authentication Issues:
- User not authenticated
- Authentication middleware failing
- Missing user context in API
-
API Implementation Issues:
- Error in API endpoint logic
- Prisma query failure
- Unhandled exceptions
Debugging Steps
Step 1: Test Database Connection and Data
Run the database test script:
npm run test-api
This will:
- Test database connection
- Check if
notification_templatestable exists - Show current data count
- Simulate the API logic
- Display the expected response structure
Step 2: Add Sample Data
If the table is empty, seed it with sample data:
npm run seed-templates
This will add 4 sample notification templates for testing.
Step 3: Run Both Tests
Run the combined debugging command:
npm run debug-templates
This runs both the test and seeding scripts.
Step 4: Check Authentication
Ensure you are logged in when testing the frontend. The API requires authentication.
Frontend Fixes Applied
The frontend code has been updated with better error handling:
- Defensive Programming: Check if response structure exists before accessing
- Better Error Messages: More specific error messages based on response type
- Empty State Handling: Properly handle empty arrays
- Loading State Management: Consistent loading state management
API Response Structure
The API should return:
{
success: true,
data: {
templates: [
{
id: "uuid",
title: "Template Name",
value: "template_value",
description: "Description",
subject: "Email Subject",
category: "category",
channels: ["email", "push"],
status: "Active",
version: "1.0",
// ... other fields
}
],
pagination: {
page: 1,
limit: 10,
totalCount: 5,
totalPages: 1,
hasNextPage: false,
hasPrevPage: false
}
}
}
Quick Fix Summary
- Frontend: Added robust error handling to prevent crashes
- Testing: Created scripts to test database and API logic
- Sample Data: Created seeding script for testing
- Debugging: Added comprehensive logging and error reporting
Next Steps
- Run
npm run debug-templatesto diagnose the issue - Check the console output for specific errors
- If database connection fails, check your
DATABASE_URLenvironment variable - If authentication fails, ensure you're logged in
- If the API works in testing but fails in the browser, check browser console for network errors
Files Modified
pages/notification/templates/index.vue- Added error handlingscripts/seed-templates.js- Sample data seedingscripts/test-api.js- Database and API testingpackage.json- Added debugging scripts
Environment Requirements
- Database server running
- Valid
DATABASE_URLin environment - Prisma client generated (
npx prisma generate) - Database migrations applied
Support
If the issue persists after following these steps, check:
- Database server status
- Environment variables
- Prisma schema sync with database
- Network connectivity
- Authentication middleware configuration