title: Trying to get current logged in user tasks in Nodejs and MongoDB
body: <p>I'm working on a project that enables the admin to assign tasks to different users, every users should only see his own tasks.</p>
  <p>I tried doing that by using the user.id as key, when the user logs in we send a token, and that token includes the user_id and other user info, I'm trying to extract the id from that token and view tasks based on that.</p>
  <p>Tasks Model</p>
  <pre><code>const TaksSchema = new Schema({
    user: {
      type: mongoose.Schema.Types.ObjectId,
      ref: 'users',
    },
    taskName: {
      name: String,
    },
    taskDesc: {
      name: String,
    },
    dateAssigned: {
      type: Date,
      default: Date.now,
    },
    requiredDate: {
      type: Date,
    },
    completed: { type: Boolean, default: false },
  });
  
  // Export Schema
  module.exports = Tasks = mongoose.model('tasks', TaksSchema); 
  </code></pre>
  <p>User model</p>
  <pre><code>const UserSchema = new Schema({
    name: {
      type: String,
      required: true,
    },
    email: {
      type: String,
      required: true,
    },
    password: {
      type: String,
      required: true,
    },
    role: {
      type: String,
      enum: ['basic', 'admin'],
      default: 'basic',
    },
    avatar: {
      type: String,
    },
    date: {
      type: Date,
      default: Date.now,
    },
  });
  
  // Export Schema
  module.exports = User = mongoose.model('users', UserSchema);
  </code></pre>
  <p>tasks route</p>
  <pre><code>router.get('/', (req, res) =&gt; {
    const errors = {};
    Tasks.findOne({ user: req.user.id })
      .populate('user', ['name', 'avatar'])
      .then((task) =&gt; {
        if (!task) {
          errors.notask = &quot;There's no Tasks Right Now&quot;;
          return res.status(400).json(errors);
        }
        res.json(task).catch((err) =&gt; res.status(404).json(err));
      });
  });
  </code></pre>
  <p>When I try sending the get request from postman I get this error</p>
  <blockquote>
  <p>TypeError: Cannot read property 'id' of undefined</p>
  </blockquote>
  <p>For security I'm sending the Id through JWT token.
  Here is the code</p>
  <pre><code>const payload = { id: user.id, name: user.name, avatar: user.avatar }; // Create jwt patload
          // Sign the token
          jwt.sign(
            payload,
            keys.secretOrKey,
            { expiresIn: 3600 },
            (err, token) =&gt; {
              res.json({ sucess: true, token: 'Bearer ' + token });
            }
          );
  </code></pre>