【问题标题】:How to get isModified to be true in mongoose with Node如何使用 Node 在 mongoose 中将 isModified 设为 true
【发布时间】:2020-04-22 08:36:38
【问题描述】:

我正在使用猫鼬模式在 pre hook 上对密码进行哈希处理,但是,我为 isModified 所做的检查是否应该使用 isModified 对密码进行哈希/重新哈希处理总是导致错误。

await mongoose.connect(this.connUri, {
    useNewUrlParser: true,
    useUnifiedTopology: true,
    useCreateIndex: true
});

const oldUser = await UserModel.findOne({ name: user.name });
oldUser.name = user.name || oldUser.name;
oldUser.password = user.password || oldUser.password;
oldUser.firstName = user.firstName || oldUser.firstName;
oldUser.lastName = user.lastName || oldUser.lastName;
oldUser.email = user.email || oldUser.email;
oldUser.status = user.status || oldUser.status;
let modified = oldUser.isModified(); // test for seeing if modified or not. Always false

await oldUser.save();
result.status = status;
result.result = oldUser;
await mongoose.disconnect();

一个 fyi user 被传递到容纳它的方法中。我会认为通过更改属性,它将被标记为isModified === true 那么如何将isModified 设置为true/false 或实际设置它?任何提示、建议或建议表示赞赏。也可以采用不同的方式进行此操作,谢谢!


编辑 - 基于 cmets 和建议的类似答案

即使使用指定的对象参数,我的代码在isModified() 方法中仍然只显示为false 而不是true。所以建议的问题没有帮助,因为它首先没有回答修改后的属性是如何设置的

如果每个属性都使用oldUser.set('password', user.password || oldUser.password);,那么对象会得到isModifed() === true,但是,当您直接使用document.property = 'some value' 设置它时,对象不会得到isModified() === true,但insetad 是false,甚至尽管它确实更改了属性,并将其保存在带有.save() 的数据库中。那么为什么会这样,用 mongoose 更新文档的更好方法是什么?

【问题讨论】:

  • 我在这里唯一没有得到的是连接字符串。您如何在函数之外使用await?连接正确吗?
  • @MattiaRasulo 容纳 sn-p 的方法是异步的,由于为此设置了 mongoose,您可以等待查询。
  • @MattiaRasulo 没有,但感谢分享。
  • @MattiaRasulo 我用更多细节更新了这个问题。你能解释一下为什么会发生这种情况以及哪种方法更好吗?

标签: javascript node.js mongodb typescript mongoose


【解决方案1】:

我从头开始制作了一个完整的工作示例来测试它,你可以在这里找到 repo:

https://github.com/ZeldOcarina/mongoose-edit-example

主要的收获是这个处理程序:

app.patch('/user', async (req, res) => {
    const { id, username, password } = req.body;
    const user = await User.findById(id);
    user.username = username ? username : user.username;
    user.password = password ? password : user.password;
    await user.save();
    res.status(200).json(user);
});

如果存在值,则表示某些内容来自表单,因此已更改,请告诉我这是否适合您,或者我会进一步研究。

【讨论】:

  • 我喜欢你在作业中使用三元组。当用户名被更改时,isModified() 会给你什么?我遇到的问题是,当更新属性时,文档应该为 isModified() 返回 true,但事实并非如此。
  • 是的,我知道。您已经要求其他方法来检查它,这可能是一种!它符合您的需求吗?
【解决方案2】:

解决方案是为什么不使用更改属性的条件。例如,如果您想像这样检查密码isModified

oldUser.pre('save', function(next) {                                                                                                                                   
  if(oldUser.password && oldUser.isModified('password')){                                                                                                                                                                                                                                                                                      
    this.password  = bcrypt.hashSync(oldUser.password, bcrypt.genSaltSync(8),null);                                                                                                             
  }
next() });  

【讨论】:

  • 请查看文档,您可以选择路径(属性)或整个文档。 mongoosejs.com/docs/api.html#document_Document-isModified
  • @TeukuMuliaIchsan 感谢您的回答,但这无助于解决根本问题。不幸的是,即使您指定了路径属性,isModified() 也始终为 false。
猜你喜欢
  • 2020-07-02
  • 2018-11-07
  • 1970-01-01
  • 2019-05-10
  • 2023-03-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多