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' });