节点保存返回 500
Node save returning 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.save() 是代码失败的地方。
我不确定这是否有必要,但这是 userModel.js 文件:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema(
{
email: {type: String, required: true},
passwordHas: {type: String, required: true},
},
{
timestamps: true,
}
);
const User = mongoose.model("user", userSchema);
module.exports = User;
根据教程,只要电子邮件和密码不存在,我应该会看到以下内容:
但是,我收到了 500 错误,我不确定为什么。终端控制台中没有可见的错误。
我该如何解决这个问题?
在您的模型中,您已将其声明为 passwordHas
,但是当您创建一个新的用户对象时,您正在创建一个名为 passwordHash
.
的 属性
const newUser = new User({
email,
passwordHash,
});
以上将生成这些属性:
{email: 'request parameter email', passwordHash: 'generated password hash'}
要么在这里修改:
const newUser = new User({
email,
passwordHas : passwordHash,
});
or change the name in your model from `passwordHas` to `passwordHash`.
我正在尝试在 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.save() 是代码失败的地方。
我不确定这是否有必要,但这是 userModel.js 文件:
const mongoose = require("mongoose");
const userSchema = new mongoose.Schema(
{
email: {type: String, required: true},
passwordHas: {type: String, required: true},
},
{
timestamps: true,
}
);
const User = mongoose.model("user", userSchema);
module.exports = User;
根据教程,只要电子邮件和密码不存在,我应该会看到以下内容:
但是,我收到了 500 错误,我不确定为什么。终端控制台中没有可见的错误。
我该如何解决这个问题?
在您的模型中,您已将其声明为 passwordHas
,但是当您创建一个新的用户对象时,您正在创建一个名为 passwordHash
.
const newUser = new User({
email,
passwordHash,
});
以上将生成这些属性:
{email: 'request parameter email', passwordHash: 'generated password hash'}
要么在这里修改:
const newUser = new User({
email,
passwordHas : passwordHash,
});
or change the name in your model from `passwordHas` to `passwordHash`.