2016-06-10 55 views
2

我在創建忘記密碼功能時可能在我的synatx中出錯。Typescript和Node js得到「不是函數錯誤」

我得到的錯誤是這個

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

回答

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變量,處理程序中的匿名函數與處理程序的範圍不同)。儘量避免這種結構。

+0

我明白你的意思,並且控制匿名函數的範圍是有意義的:)但我仍然得到相同的結果,不是函數錯誤。 –

+0

我在綁定中遇到了問題,在那裏我將錯誤的路由綁定到幫助程序 –

+0

,所以現在它正在工作? –

相關問題