2

I have an error in possibly my synatx for a forgot password feature I am creating.

The error I get is this

[TypeError: self._forgotPasswordRouteHelper.sendPasswordResetEmail is not a function]

My route looks like the below

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);
            }
        };

And in my forgotPasswordHelper.ts file I have a function like this

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);
}

I am unsure where I am going wrong? Could this possibly be a syntax error? When I post to that url in postman, I get all of the data that is needed to run this function and send the email, but I get errors at

self._forgotPasswordRouteHelper.sendPasswordResetEmail(uuid, parsedRequest.email, person.name_to_call_user);

1 Answer 1

1

Try this:

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);
            }
        };

You have nested closures (That's why I declared a new me variable in the handler, the anonymous functions in the handler does not have the same scope as the handler). Try to avoid this structure.

Sign up to request clarification or add additional context in comments.

3 Comments

I get what you are saying, and that makes sense to control the scope that the anonymous functions have :) But I still get the same, is not a function error.
I had a problem in my bindings, where I binded the wrong route to the helper
Yes, running, but the above also fixed a later issue

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.