用猫鼬验证而不保存 [英] Validate in mongoose without saving
本文介绍了用猫鼬验证而不保存的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否可以在不保存的情况下运行猫鼬验证器?基本上,想进行一次改动,并确保在选择以下选项时可以保存 :
Is it possible to run the mongoose validator without also saving? Basically, wanting to do a dry run of a change and making sure that it would save if I chose to:
myThing.validated_property = 5;
try { myThing.check(); } catch(e) { console.log("nope!") }
推荐答案
您可以调用 validate
在猫鼬文档上评估其验证规则:
You can call validate
on a Mongoose doc to evaluate its validation rules:
myThing.validated_property = 5;
myThing.validate(function(err) {
if (err) { console.log('nope!') }
});
这篇关于用猫鼬验证而不保存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文