0

iv been trying to add post action to add tasks to db , Im using mongoDB as a db.

Here is my User model:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const userSchema = new Schema({
    username: {
        type: String,
        required: true
    },
    password: {
        type: String,
        required: true
    }
})

module.exports = mongoose.model('User', userSchema);

And here is my Task model:

const mongoose = require('mongoose');

const Schema = mongoose.Schema;

const taskScheme = new Schema({
    description: {
        type: String,
        required: true
    },

    status: {
        type: String,
        required: true
    },

    userId: {
        type: mongoose.Schema.Types.ObjectId,
        ref: 'User',
        required: true
    }
})

module.exports = mongoose.model('Task', taskScheme);

And here is adminController, im getting the error inside addNewTask

const Task = require("../models/task");

exports.addNewTask = (req, res, next) => {
    const taskDesc = req.body.taskDesc;
    const userId = req.user._id;
    console.log('taskDesc is: ', taskDesc);
    console.log('req.user is: ', req.user) // Output: 'req.user is:  undefined' 
    
    const newTask = new Task(
        {
        description: taskDesc,
        status: 'In Progress',
        userId: userId //get somehow userId , but need first maybe to validate and when moving to all tasks page
        }
    );

    newTask.save()
        .then(() => {
            console.log('Task created');
            res.redirect('/');
        })
        .catch((err) => {
            console.log(err);
        })
}

//TODO: +add: check what user is connected and than deleted the task with the correct userid to match the id in the db
exports.removeTask = (req, res, next) => {
    const taskId = req.body.taskId;

    Task.findByIdAndRemove({
        _id: taskId
    })
        .then(() => {
            res.redirect('/')
        })
        .catch(err => {
            console.log('removeTask error: ', err);
        });
}
//TODO: same things as i wrote in removeTask
exports.editTaskToFinish = (req, res, next) => {
    const taskId = req.body.taskId;
    Task.findByIdAndUpdate(
        {_id: taskId},
        {$set: {status: 'Done'}})
        .then(() => {
            res.redirect('/');
        })
        .catch((err) => {
            console.log('editTaskToFinish: ', err)
            res.redirect('/');
        })
}



brute test:

addNewTask is working when iv replaced the req.user._id with the _id that appears inside the user in mongodb

so i dont really know why it cant see the _id inside the User scheme

If you any further information please let me know

2
  • Where is req.user coming from? Please post the code that shows how you are adding a user object to the req object. Commented Oct 20, 2023 at 14:37
  • The user adding part is working , iv just posted the solution. Commented Oct 20, 2023 at 14:49

1 Answer 1

0

I dont know why but changing the line:

req.user._id

to

req.session.user._id 

did the work

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, including that you were using session middleware was a fundamental part in solving your issue. When you use express-session a session object is added to the req object so thats why your user details are stored in the session object.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.