javascript - 尽管我正在对其进行变异,但对象返回相同
问题描述
即将到来的代码片段将从用户 JSON 对象中删除密码属性并返回它作为响应。发生的事情是密码属性仍在返回!
const signin = (req, res, next) => {
let requestBody = req.body;
userModel.findUserByEmail(requestBody.email).then(user => {
bcrypt.compare(requestBody.password, user.password, (error, result) => {
if (!result) {
return res.status(500).json({
status: false,
message: 'Auth Failed!',
error
});
}
if (error) {
return res.status(500).json({
error
});
}
let token = jwt.sign({
email: user.email,
userId: user._id
},
process.env.JWT_KEY,
{
expiresIn: "2h"
});
// remonve password key
delete user.password
res.status(200).json({
status: true,
message: 'Authenticated!',
data: {
token,
user
}
});
});
}).catch(error => {
return res.status(500).json({
status: false,
message: 'Auth Failed!',
error
});
});
}
不确定问题是否与异步编译有关
解决方案
您可以在没有密码的情况下创建一个新对象并在您的响应中使用它:
const { password, ...restOfUser } = user
res.status(200).json({
status: true,
message: 'Authenticated!',
data: {
token
user: restOfUser
}
})
推荐阅读
- java - 使用一个“sendPost”功能发送多个请求
- sas - SAS 字符比较
- windows - 如何使用 CMD 和 reg.exe 添加具有包含空格和参数的长路径的程序可执行文件?
- firebase - 如何在日期为 datenow 的 firebase 选择条件中查询
- c# - 在使用 MVVM 单击 ListView 项目内的按钮时选择 ListView 中的项目
- scrapy - 如何使用 Scrapyd 和 ScrapydWeb 在集群中分布蜘蛛?
- ios - 在 IOS 13 中,当 ViewController 未推送时,Google Map 相机位置未显示准确。附上两个屏幕截图
- assembly - 在 GAS .intel-syntax 程序集中的内存标签和寄存器上使用“偏移”和“[]”有什么区别?
- firebase - 为什么 Firebase 身份验证在没有 google dns 的情况下不起作用?
- python - python中所有可能的组合