Steven Talafous
Steven Talafous

Reputation: 107

Display Mongo user data in EJS file with node/express

I'm running node/express/mongoose/mongo and I'm trying to display a logged-in user's info in their account page.

In my user controller file, I'm trying to access their Mongo User ID to then look up the full User object, load that object into a const, and pass that object into the render of an EJS file.

const User = require('../models/user');

module.exports.account = async (req, res) => {
    const userID = req.user._id;
    const { user } = await User.findById(userID);
    res.render('app/account', { user })
}

When I console.log userID, I get "new ObjectId("61d321284256dffb7e869e8f")" (shouldn't I just get the number string?). When I console.log user.email when hard coding findByID('61d321284256dffb7e869e8f'), I get the error "TypeError: Cannot read properties of undefined".

Really lost on this one so I appreciate any help here.

Upvotes: 0

Views: 681

Answers (1)

Steven Talafous
Steven Talafous

Reputation: 107

Answered - added toString in the code and also found that I used,

module.exports.account = async (req, res) => {
    const userID = req.user._id.toString();
    const user = await User.findById(userID);
    res.render('app/account', { user })
}

Upvotes: 1

Related Questions