From 22f2155cfc95c9e4d52b7dcf280117fad7185960 Mon Sep 17 00:00:00 2001 From: javdiaort <javdiaort@alum.us.es> Date: Wed, 19 Mar 2025 10:37:34 +0100 Subject: [PATCH] usuario ve sus tareas. --- src/index.js | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 8b1ce00..d6bbf88 100644 --- a/src/index.js +++ b/src/index.js @@ -66,8 +66,7 @@ if(existingUser){ } }) - -//Login User +//login app.post("/login", async (req, res) => { try { const { username, password } = req.body; @@ -76,10 +75,6 @@ app.post("/login", async (req, res) => { if (!user) { return res.send("Usuario no encontrado"); } - // Asegúrate de que tasks sea un array vacÃo si no existe - if (!user.tasks) { - user.tasks = []; - } const isPasswordMatch = await bcrypt.compare(password, user.password); if (!isPasswordMatch) { @@ -87,12 +82,15 @@ app.post("/login", async (req, res) => { } // Guardar el ID del usuario en la sesión - req.session.userId = user._id; + req.session.userId = user._id; console.log("Sesión después de asignar userId:", req.session); - // Redirigir al To-Do List (home.ejs) - res.render("home", { tasks: user.tasks }); + // 🔹 Obtener las tareas del usuario desde la colección "tasks" + const tasks = await Task.find({ userId: user._id }); + + // Redirigir al To-Do List con las tareas cargadas + res.render("home", { tasks }); } catch (error) { console.error("Error en login:", error); @@ -103,6 +101,7 @@ app.post("/login", async (req, res) => { + // PARTE TODO LIST app.get("/home", async (req, res) => { if (!req.session.userId) { @@ -110,8 +109,11 @@ app.get("/home", async (req, res) => { } try { - // Obtener las tareas del usuario - const tasks = await Task.find({ userId: req.session.userId }); + // Convertir userId en un ObjectId válido + const userId = new mongoose.Types.ObjectId(req.session.userId); + + // Obtener las tareas del usuario desde MongoDB + const tasks = await Task.find({ userId }); // Mostrar las tareas en la vista res.render("home", { tasks }); -- GitLab