Skip to content

Commit f1453f8

Browse files
committed
Fix Database page rendering crashes and logic errors
- Resolved ReferenceError by moving `fetchData` definition before `useEffect`. - Added safety checks for `activeCollection.model` to prevent crashes on undefined models. - Fixed CSS class typo on "Add Record" button. - Removed duplicate table headers and cells.
1 parent 6a58f91 commit f1453f8

1 file changed

Lines changed: 12 additions & 12 deletions

File tree

‎frontend/src/pages/Database.jsx‎

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,6 @@ export default function Database() {
5050
fetchProject();
5151
}, [projectId, token, searchParams]);
5252

53-
// Fetch Data on Collection Change
54-
useEffect(() => {
55-
if (!activeCollection) return;
56-
setSearchParams({ collection: activeCollection.name });
57-
fetchData();
58-
if (window.innerWidth <= 768) setIsSidebarOpen(false);
59-
}, [activeCollection, fetchData, setSearchParams]);
60-
6153
const fetchData = useCallback(async () => {
6254
if (!activeCollection) return;
6355
setLoadingData(true);
@@ -74,6 +66,14 @@ export default function Database() {
7466
}
7567
}, [activeCollection, projectId, token]);
7668

69+
// Fetch Data on Collection Change
70+
useEffect(() => {
71+
if (!activeCollection) return;
72+
setSearchParams({ collection: activeCollection.name });
73+
fetchData();
74+
if (window.innerWidth <= 768) setIsSidebarOpen(false);
75+
}, [activeCollection, fetchData, setSearchParams]);
76+
7777
const handleDelete = async (id) => {
7878
if (!window.confirm("Are you sure you want to delete this document?")) return;
7979
try {
@@ -92,7 +92,7 @@ export default function Database() {
9292
e.preventDefault();
9393
try {
9494
const formattedData = { ...newData };
95-
activeCollection.model.forEach(field => {
95+
(activeCollection.model || []).forEach(field => {
9696
if (field.type === 'Number') formattedData[field.key] = Number(formattedData[field.key]);
9797
if (field.type === 'Boolean') formattedData[field.key] = formattedData[field.key] === 'true';
9898
});
@@ -201,7 +201,7 @@ export default function Database() {
201201
<thead>
202202
<tr>
203203
<th className="w-12">#</th>
204-
{activeCollection.model.map(field => (
204+
{(activeCollection.model || []).map(field => (
205205
<th key={field.key}>
206206
<div className="th-content">
207207
{field.key}
@@ -217,7 +217,7 @@ export default function Database() {
217217
{data.map((row, i) => (
218218
<tr key={row._id} className="table-row">
219219
<td className="text-muted">{i + 1}</td>
220-
{activeCollection.model.map(field => (
220+
{(activeCollection.model || []).map(field => (
221221
<td key={field.key}>
222222
{typeof row[field.key] === 'boolean' ? (
223223
<span className={`status-badge ${row[field.key] ? 'success' : 'danger'}`}>
@@ -364,7 +364,7 @@ export default function Database() {
364364
</button>
365365
</div>
366366
<form onSubmit={handleAddDocument} className="modal-body">
367-
{activeCollection.model.map(field => (
367+
{(activeCollection.model || []).map(field => (
368368
<div key={field.key} className="form-group">
369369
<label className="form-label">
370370
{field.key}

0 commit comments

Comments
 (0)