From cc3c776f68ea28e06a5d6888edf13641546c0b04 Mon Sep 17 00:00:00 2001 From: Claude Date: Tue, 11 Nov 2025 11:27:45 +0000 Subject: [PATCH] =?UTF-8?q?Correction=20de=20l'erreur=20500=20sur=20/api/n?= =?UTF-8?q?otes=20et=20gestion=20robuste=20c=C3=B4t=C3=A9=20client?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Remplacement de COUNT(DISTINCT) par une sous-requête dans SQLite - Ajout de vérification Array.isArray côté client - Gestion d'erreur améliorée avec fallback sur tableau vide - Correction du bug 'filtered.forEach is not a function' - L'API retourne toujours un tableau, même en cas d'erreur --- public/js/complete-app.js | 11 ++++++++++- routes/notes.routes.js | 6 ++---- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/public/js/complete-app.js b/public/js/complete-app.js index 8d80afe..e33a060 100644 --- a/public/js/complete-app.js +++ b/public/js/complete-app.js @@ -184,10 +184,14 @@ async function initAuth() { // ==================== NOTES ==================== async function loadNotes() { try { - state.notes = await api.get('/api/notes'); + const response = await api.get('/api/notes'); + // S'assurer que response est un tableau + state.notes = Array.isArray(response) ? response : []; renderNotes(); } catch (error) { console.error('Erreur chargement notes:', error); + state.notes = []; + renderNotes(); } } @@ -195,6 +199,11 @@ function renderNotes() { const notesGrid = document.getElementById('notesGrid'); if (!notesGrid) return; + // S'assurer que state.notes est toujours un tableau + if (!Array.isArray(state.notes)) { + state.notes = []; + } + let filtered = state.notes; // Filtre de recherche diff --git a/routes/notes.routes.js b/routes/notes.routes.js index 931af4c..f5ad74f 100644 --- a/routes/notes.routes.js +++ b/routes/notes.routes.js @@ -48,15 +48,13 @@ router.get('/', async (req, res) => { SELECT n.id, n.title, n.content, n.image_filename, n.created_at, n.updated_at, - COUNT(DISTINCT nt.id) as todos_count + (SELECT COUNT(*) FROM note_todos WHERE note_id = n.id) as todos_count FROM notes n - LEFT JOIN note_todos nt ON n.id = nt.note_id WHERE n.user_id = ? - GROUP BY n.id ORDER BY n.updated_at DESC `, [req.user.id]); - res.json(notes); + res.json(notes || []); } catch (error) { logger.error('Erreur lors de la récupération des notes:', error); res.status(500).json({ error: 'Erreur serveur' });