猫鼬:发送到客户端后无法设置 headers
Mongoose: cannot set headers after they are sent to the client
我正在尝试使用 nodejs-mongodb 构建登录系统。当我 post 字段时,我的控制器找到了用户,但它不允许我使用 res.send() catch 块处理这里我的代码:
控制器
exports.loginUser = async (req, res) => {
try {
const {email,password} = req.body;
await User.findOne({email},(err,user) => {
if(user){
bcrypt.compare(password, user.password,(err,same) => {
if(same){
res.status(200).json({
status:"Login Success"
});
}
});
}
});
} catch(error){
res.status(400).json({
status: "Failed",
error
})
}
};
架构
const UserSchema = new Schema({
name:{
type:String,
required:true
},
email:{
type:String,
required:true,
unique:true
},
password:{
type:String,
required:true
}
});
我删除了 findOne() 方法的回调函数并使用了 .then()。解决了。
我正在尝试使用 nodejs-mongodb 构建登录系统。当我 post 字段时,我的控制器找到了用户,但它不允许我使用 res.send() catch 块处理这里我的代码:
控制器
exports.loginUser = async (req, res) => {
try {
const {email,password} = req.body;
await User.findOne({email},(err,user) => {
if(user){
bcrypt.compare(password, user.password,(err,same) => {
if(same){
res.status(200).json({
status:"Login Success"
});
}
});
}
});
} catch(error){
res.status(400).json({
status: "Failed",
error
})
}
};
架构
const UserSchema = new Schema({
name:{
type:String,
required:true
},
email:{
type:String,
required:true,
unique:true
},
password:{
type:String,
required:true
}
});
我删除了 findOne() 方法的回调函数并使用了 .then()。解决了。