如何解决节点保存返回 500
我正在尝试在 Udemy 上完成 MERN 课程,但在用户身份验证方面遇到了困难。
对于本节,我使用 Insomnia 处理各种请求,但由于某些原因,我得到的结果与教程不同。
这是我的 userRouter.js 代码:
const router = require("express").Router();
const User = require("../models/userModel");
const bcrypt = require("bcryptjs");
router.post("/",async (req,res) => {
try {
const {email,password,passwordVerify} = req.body;
const existingUser = await User.findOne({email});
if (existingUser) {
return res.status(400).json({
errorMessage: "An account with the email already exists.",});
}
const salt = await bcrypt.genSalt();
const passwordHash = await bcrypt.hash(password,salt);
const newUser = new User({
email,passwordHash,});
// the below 2 lines seems to be the cause of the error
const savedUser = await newUser.save();
res.send(savedUser);
}
catch (err) {
res.status(500).send();
}
});
module.exports = router;
如上所示,try 块的最后两行是代码失败的地方。我可以通过 console.log(newUser) 查看 newUser。
但是 newUser.save() 是代码失败的地方。
我不确定这是否有必要,但这里是 userModel.js 文件:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema(
{
email: {type: String,required: true},passwordHas: {type: String,},{
timestamps: true,}
);
const User = mongoose.model("user",userSchema);
module.exports = User;
根据教程,只要电子邮件和密码不存在,我应该看到以下内容:
但相反,我收到了 500 错误,我不知道为什么。终端控制台中没有可见的错误。
我该如何解决这个问题?
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。