我有一个预保存挂钩来加密模式password字段User,例如:
var schema = new mongoose.Schema({
username: 'string',
password: 'string'
});
schema.pre('save', encrptPasswordHook);
schema.pre('update', encrptPasswordHook);
schema.pre('findOneAndUpdate', encrptPasswordHook);
...
Run Code Online (Sandbox Code Playgroud)
通过这种方式,每次User创建或更新时,我都会在我的数据库中加密密码字符串。
现在我有一个User带有加密密码的旧数据的 JSON 文件。我想使用这个User模型将 JSON 文件导入我的数据库。
如何避免预存钩子再次加密密码?
我正在尝试在客户端使用 fetch() 将数据发送到我的 NodeJS 服务器或从我的 NodeJS 服务器发送数据。
服务器很好地收到了发布请求,并且我能够记录 req 变量,但是当我 res.send('any data') 时,客户端无法检测到数据。奇怪的是,chrome 可以看到响应,但我根本不知道如何引用数据!
客户端代码
fetch('/',{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
user:{
name: 'John',
email: 'J@B.com',
}
})
.then(res => console.log(res))
.then(data => console.log(data))
.catch((error) => console.error('Error:',error))
Run Code Online (Sandbox Code Playgroud)
服务器代码
app.post('/', (req,res) => {
console.log(req.body.user)
res.send('hello?')
})
Run Code Online (Sandbox Code Playgroud)