[英]mocha test fail because this is undefined in mongoose pre validate hooks model (even without arrow function)
我正在测试一个基本上将用户注册到db的发布请求。 我的Mocha测试失败,因为触发预验证钩子时未定义。 我已经在使用普通功能而不是箭头功能。 它在postman/insomnia
非常有效。 如果我通过摩卡咖啡进行测试,则会出现问题。
userSchema.pre('validate', function(next) {
if (this.password.split(' ').length > 1) {
next(new Error('password can not contain a white space'));
} else {
next();
}
});
要访问它,它应该是单独的功能
function preValidateFunction(next) {
if (this.password.split(' ').length > 1) {
next(new Error('password can not contain a white space'));
} else {
next();
}
}
userSchema.pre('validate', preValidateFunction);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.