Home > other >  "TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')" erro
"TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')" erro

Time:06-19

EDIT: PROBLEM SOLVED.

I'm trying to make an update profile page for an Express app with Mongoose, and I got the error "TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')". I am confused how to fix it. Here is my code, thanks

exports.editProfilePost = async (req, res, next) => {
  try {
    const username = req.user.username;
    const user = await User.findOneAndUpdate({ username: username }, req.body, {
      new: true,
    });
    res.redirect('/profile');
  } catch (error) {
    next(error);
  }
};

(error shown) [1]: https://i.stack.imgur.com/xss8j.png

CodePudding user response:

The req.user could be undefined. To debug, add the checking:

let username = "";
if (typeof req.user !== "undefined") {
   username = req.user.username;
}

CodePudding user response:

So... idk why this works but I forgot to make the profile picture push to cloudinary. Somehow, not pushing to cloudinary made the req.body empty, resulting in the error.

  • Related