簡體   English   中英

“TypeError:無法讀取未定義的屬性(讀取'hasOwnProperty')”錯誤

[英]"TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')" error

編輯:問題已解決。

我正在嘗試使用 Mongoose 為 Express 應用程序創建更新配置文件頁面,但出現錯誤“TypeError: Cannot read properties of undefined (reading 'hasOwnProperty')”。 我很困惑如何解決它。 這是我的代碼,謝謝

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);
  }
};

(顯示錯誤)[1]: https ://i.stack.imgur.com/xss8j.png

req.user 可能未定義。 要調試,請添加檢查:

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

所以......我知道為什么這有效,但我忘記將個人資料圖片推送到雲端。 不知何故,不推送到 cloudinary 使 req.body 為空,從而導致錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM