尝试使用该POST
方法postman
注册用户后出现错误。我Schema
为register
路线创建了一个简单的。
错误:
Error in register: Error: User validation failed: password: Cast to string failed for value "Promise { <pending> }" (type Promise) at path "password"
at ValidationError.inspect (/Users/*****/authSystem/node_modules/mongoose/lib/error/validation.js:48:26)
at formatValue (node:internal/util/inspect:782:19)
at inspect (node:internal/util/inspect:347:10)
at formatWithOptionsInternal (node:internal/util/inspect:2167:40)
at formatWithOptions (node:internal/util/inspect:2029:10)
at console.value (node:internal/console/constructor:324:14)
at console.log (node:internal/console/constructor:360:61)
at /Users/harshmishra/Desktop/BackendApp/authSystem/app.js:46:17
at processTicksAndRejections (node:internal/process/task_queues:96:5) {
errors: {
password: CastError: Cast to string failed for value "Promise { <pending> }" (type Promise) at path "password"
at SchemaString.cast (/Users/*****/authSystem/node_modules/mongoose/lib/schema/string.js:600:11)
at SchemaString.SchemaType.applySetters (/Users/*****/authSystem/node_modules/mongoose/lib/schematype.js:1189:12)
at model.$set (/Users/***/authSystem/node_modules/mongoose/lib/document.js:1409:20)
at model.$set (/Users/*****/authSystem/node_modules/mongoose/lib/document.js:1137:16)
at model.Document (/Users/****/authSystem/node_modules/mongoose/lib/document.js:162:12)
at model.Model (/Users/****/authSystem/node_modules/mongoose/lib/model.js:115:12)
at new model (/Users/*****/authSystem/node_modules/mongoose/lib/model.js:4825:15)
at /Users*****/authSystem/node_modules/mongoose/lib/model.js:3132:22
at /Users/*****/authSystem/node_modules/mongoose/lib/model.js:3168:7
at Array.forEach (<anonymous>) {
stringValue: '"Promise { <pending> }"',
messageFormat: undefined,
kind: 'string',
value: [Promise],
path: 'password',
reason: null,
valueType: 'Promise'
}
},
_message: 'User validation failed'
}
架构:
const userSchema = new mongoose.Schema(
{
firstName: {
type: String,
default: null,
},
lastName: {
type: String,
default: null,
},
email: {
type: String,
unique: true,
},
password: {
type: String,
},
token: {
type: String,
},
},
);
module.exports = mongoose.model('User', userSchema)
/登记:
app.post("/register", async(req, res) => {
try {
const { firstName, lastName, email, password } = req.body;
if (!(firstName && lastName && email && password)) {
return res.status(400).send("All fields are required");
}
const existingUser =User.findOne({ email });
if (existingUser) {
return res.status(401).send("User already exist");
}
const myEncrpytedPass = await bcryptjs.hash(password, 10);
const user = await User.create({
firstName,
lastName,
email: email.toLowerCase(),
password: myEncrpytedPass,
});
const token = await jwt.sign(
{
user_id: user._id,
email,
},
process.env.SECRET_KEY,
{
expiresIn: "4h",
}
);
user.token = token;
return res.status(201).json(user);
} catch (error) {
console.log("Error in register:",error)
}
})
我无法弄清楚我在这里做错了什么?正如我遵循猫鼬的文档,但我仍然收到错误。
我应该在这里做些什么改变?
您需要更改两件事:
尝试await
在bcryptjs.hash()
. 看起来该函数返回了一个 Promise。
const myEncrpytedPass = await bcryptjs.hash(password, 10);
在return
每个res.send
.
例如,如果用户存在,您将发送 this res.send(401).send("User already exist")
,但由于您没有添加return
,功能将继续,并会到达下一个res.status(201).json(user)
响应。这将引发错误,Cannot set headers after they are sent to the client
因为您已经将响应发送给用户。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句