我的synatx中有一个错误,我正在创建忘记密码功能。
我得到的错误就是这个
[TypeError: self._forgotPasswordRouteHelper.sendPasswordResetEmail is not a function]
我的路线如下所示
getRoute(): hapi.IRouteConfiguration {
const self = this;
return {
method: 'POST',
path: this._config.apiPrefix + 'forgotpassword',
handler: function(request: hapi.Request, reply: hapi.IReply) {
let parsedRequest: IForgotPasswordDataRequest = null;
let uuid: string;
const result = new Promise<string>(function(resolve, reject) {
self._validator.validate(request.payload, ForgotPasswordDataRequestValidator)
.then(function(validationResult) {
if (validationResult.error) {
resolve(responseHelper.getErrorResponse(ResponseErrorCode.invalidRequest));
throw null;
}
parsedRequest = <IForgotPasswordDataRequest> request.payload;
return self._forgotPasswordQueries.createEmailResetToken(parsedRequest.email);
}
, function(error) {
console.error(error);
resolve(responseHelper.getErrorResponse(ResponseErrorCode.invalidRequest));
throw null;
})
.then(function(newUuid: string) {
uuid = newUuid;
return self._peopleQueries.getPersonByEmail(parsedRequest.email);
})
.then(function(person: IPersonModel) {
resolve(responseHelper.getSuccessResponse(null, null));
self._forgotPasswordRouteHelper.sendPasswordResetEmail(uuid, parsedRequest.email, person.name_to_call_user);
})
.catch(function(error: any) {
console.error(error);
resolve(responseHelper.getErrorResponse(ResponseErrorCode.unknownError));
});
});
reply(result);
}
};
在我的forgotPasswordHelper.ts文件中,我有一个像这样的函数
sendPasswordResetEmail(person: any): Promise<void> {
const token = person.forgot_password_token.dataValues.forgot_password_tokens_id;
const data: IForgotPasswordEmailEmailData = {
'resetUrl': this._config.websiteUrl + 'reset_password/confirm/' + token,
'emailTo': person.contact_detail.value,
'name': person.person.name_to_call_user
}
return this._emailHelper.sendPasswordResetEmail(data);
}
我不确定我哪里出错了?这可能是语法错误吗?当我在邮递员中发布该网址时,我会获得运行此功能并发送电子邮件所需的所有数据,但是我收到了错误
self._forgotPasswordRouteHelper.sendPasswordResetEmail(uuid, parsedRequest.email, person.name_to_call_user);
答案 0 :(得分:1)
试试这个:
getRoute(): hapi.IRouteConfiguration {
const self = this;
return {
method: 'POST',
path: this._config.apiPrefix + 'forgotpassword',
handler: function(request: hapi.Request, reply: hapi.IReply) {
const me = self;
let parsedRequest: IForgotPasswordDataRequest = null;
let uuid: string;
const result = new Promise<string>(function(resolve, reject) {
me._validator.validate(request.payload, ForgotPasswordDataRequestValidator)
.then(function(validationResult) {
if (validationResult.error) {
resolve(responseHelper.getErrorResponse(ResponseErrorCode.invalidRequest));
throw null;
}
parsedRequest = <IForgotPasswordDataRequest> request.payload;
return self._forgotPasswordQueries.createEmailResetToken(parsedRequest.email);
}
, function(error) {
console.error(error);
resolve(responseHelper.getErrorResponse(ResponseErrorCode.invalidRequest));
throw null;
})
.then(function(newUuid: string) {
uuid = newUuid;
return me._peopleQueries.getPersonByEmail(parsedRequest.email);
})
.then(function(person: IPersonModel) {
resolve(responseHelper.getSuccessResponse(null, null));
me._forgotPasswordRouteHelper.sendPasswordResetEmail(uuid, parsedRequest.email, person.name_to_call_user);
})
.catch(function(error: any) {
console.error(error);
resolve(responseHelper.getErrorResponse(ResponseErrorCode.unknownError));
});
});
reply(result);
}
};
你有嵌套的闭包(这就是为什么我在处理程序中声明了一个新的me
变量,处理程序中的匿名函数与处理程序的作用域不同)。尽量避免这种结构。