大佬教程收集整理的这篇文章主要介绍了node.js – 为什么我不能在mongoose中验证嵌入式文档?这样做的正确方法是什么?,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
var testSchema = new Schema({ foo: { type: String,required: true,trim: true },bar: { fooBar: { type: String },barFoo: { type: String } } });
我必须根据foo值验证bar的值,如下所示:
testSchema.path("bar").validate(function(bar){ if(this.foo === "someValue") //return custom validation logic 1 else if(this.foo === "anotherString") //return custom validation logic 2 else return false; });
但当我尝试strat我的应用程序时,我收到以下错误:
/Users/Renato/github/local/prv/domain/models/testModel.js:34 testSchema.path("bar").validate(function(bar){ ^ TypeError: Cannot call method 'validate' of undefined
我在这做错了什么?什么是验证这个对象的正确方法???我用Google搜索,但我似乎找不到任何东西!甚至将我的猫鼬版本更新为~3.5.5
testSchema.path("bar.fooBar").validate(function(fooBar){ if(this.foo === "someValue") //return custom validation logic 1 else return false; }); testSchema.path("bar.barFoo").validate(function(barFoo){ if(this.foo === "anotherString") //return custom validation logic 2 else return false; });
您还可以找到schema.pre()
用于集体验证模型(另一个示例可以在Sub Docs文档中找到):
testSchema.pre('save',function (next) { if(this.foo === "someValue") return next(new Error('Invalid 1')); else if(this.foo === "anotherString") return next(new Error('Invalid 2')); else next(); });
以上是大佬教程为你收集整理的node.js – 为什么我不能在mongoose中验证嵌入式文档?这样做的正确方法是什么?全部内容,希望文章能够帮你解决node.js – 为什么我不能在mongoose中验证嵌入式文档?这样做的正确方法是什么?所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。