# Cannot read properties of undefined (reading '\_id')

**URL:** <https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554>\
**Category:** Node\
**Created:** [March 6, 2023, 1:48am UTC](https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554 "2023-03-06T01:48:10Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jtremochajr](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jtremochajr](https://forum.codewithmosh.com/u/jtremochajr)\
**Post date:** [March 6, 2023, 1:48am UTC](https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554/1 "2023-03-06T01:48:10Z")

</div>

Hi Mosh,

Got an error while getting the current user using GET method in postman. Here’s the error

NodeJs/vidly/routes/users.js:10  
const user = await User.findById(req.user.\_id).select(‘-password’);  
^  
TypeError: Cannot read properties of undefined (reading ‘\_id’)

const mongoose = require(‘mongoose’);  
const express = require(‘express’);  
const router = express.Router();  
const \_ = require(‘lodash’);  
const bcrypt = require(‘bcrypt’);  
const auth = require(‘…/middleware/auth’);  
const { User, validate } = require(‘…/models/user’);

router.get(‘/me’, auth, async (req, res) =\> {  
const user = await User.findById(req.user.\_id).select(‘-password’);  
res.send(user);  
});

---

<div class="post-metadata">

**Author:** ![jtremochajr](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jtremochajr](https://forum.codewithmosh.com/u/jtremochajr)\
**Post date:** [March 6, 2023, 4:18am UTC](https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554/2 "2023-03-06T04:18:16Z")

</div>

I’m able to fixed the problem using res.user.\_id instead of req.user.\_id. Im still confused because the middleware authorization return a res.user as shown

const jwt = require(‘jsonwebtoken’);  
const config = require(‘config’);

module.exports = function (req, res, next) {  
const token = req.header(‘x-auth-token’);  
if (!token) return res.status(401).send(‘Access denied. No token provided.’);

try {  
const decoded = jwt.verify(token, config.get(‘jwtPrivateKey’));  
**res.user = decoded;**  
next();  
} catch (ex) {  
res.status(400).send(‘Invalid token.’);  
}  
};

---

<div class="post-metadata">

**Author:** ![Harvel](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/harvel/32/7651_2.png) [@Harvel](https://forum.codewithmosh.com/u/Harvel)\
**Post date:** [March 6, 2023, 1:42pm UTC](https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554/3 "2023-03-06T13:42:48Z")

</div>

Hey its Harvel,  
Actually you set the user object on the response object instead of the request object,  
So req.user is undefined

---

<div class="post-metadata">

**Author:** ![Harvel](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.codewithmosh.com/harvel/32/7651_2.png) [@Harvel](https://forum.codewithmosh.com/u/Harvel)\
**Post date:** [March 6, 2023, 1:43pm UTC](https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554/4 "2023-03-06T13:43:34Z")

</div>

Set the user object on the request object in the auth middleware

---

<div class="post-metadata">

**Author:** ![jtremochajr](https://avatars.discourse-cdn.com/v4/letter/j/278dde/32.png) [@jtremochajr](https://forum.codewithmosh.com/u/jtremochajr)\
**Post date:** [March 7, 2023, 5:06am UTC](https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554/5 "2023-03-07T05:06:49Z")

</div>

Hi Harvel,

my fault actually when I review that section yesterday. my bad. Thanks anyway.

---

<div class="post-metadata">

**Author:** ![Dsoeiro](https://avatars.discourse-cdn.com/v4/letter/d/0ea827/32.png) [@Dsoeiro](https://forum.codewithmosh.com/u/Dsoeiro)\
**Post date:** [February 8, 2024, 12:40pm UTC](https://forum.codewithmosh.com/t/cannot-read-properties-of-undefined-reading-id/18554/6 "2024-02-08T12:40:17Z")

</div>

Hi, I’m with the exact same problem, I verified and I have the code exactly as you have it there but it stills shows me the error
