2016-11-08 68 views
1

我正在使用Javascript/Typescript創建返回的Promise和已格式化的PersonModel對象。但是我得到生成錯誤:Javascript Promise calling promise confusion

PersonService.ts

private encryptPerson(person: PersonModel): Promise<PersonModel> { 
    return new Promise<PersonModel>(resolve => { // <== line 332 
     let password: string = person.password; 
     this.encrypt(password).then((ciphertext: string) => { 
      person.password = ciphertext; 
      resolve(person); 
     }); 
    }, 
     error => { 
      console.error(error) 
     }); 
} 

private encrypt(value: string): Promise<string> { 
    return new Promise<string>(resolve => { 
     this.encrypter.encrypt(value).then((result) => { 
      resolve(result); 
     }, 
      error => { 
       console.error(error) 
      }); 
    }); 
} 

錯誤

ERROR in ./app/pages/service/personService.ts 
(332,16): error TS2346: Supplied parameters do not match any signature of call target. 

的我應該如何構建這種讚賞任何幫助。

UPDATE

從T.J.提醒後下面克勞德,我有以下幾點:

private encryptPerson(person: PersonModel): Promise<PersonModel> { 
    return new Promise<PersonModel>(resolve => { 
     let password: string = person.password; 
     this.encrypt(password).then((ciphertext: string) => { 
      person.password = ciphertext; 
      resolve(person); 
     }); 
    }); 
} 

private encrypt(value: string): Promise<string> { 
    return new Promise<string>(resolve => { 
     this.encrypter.encrypt(value).then((result: string) => { 
      resolve(result); 
     }); 
    }); 
} 
+1

您正在調用傳遞給調用目標的參數。指定在 – Justinas

+0

上發生此錯誤的行@Justinas:它有點隱藏,但他們已經在代碼註釋中指出哪一行是第332行。 –

+0

@Justina,請參閱以下內容:'// <== line 332' – Richard

回答

2

你調用Promise構造與2個參數:

private encryptPerson(person: PersonModel): Promise<PersonModel> { 
    return new Promise<PersonModel>(resolve => { // <== line 332 
      let password: string = person.password; 
      this.encrypt(password).then((ciphertext: string) => { 
       person.password = ciphertext; 
       resolve(person); 
      }); 
     }, 
     error => {     // ** This is the 
      console.error(error) // ** second argument 
     });       // ** to the constructor 
} 

只需要一個參數。我懷疑第二個箭頭函數是爲了附在第一個裏面的then

另外,如果我正確地指出第二個函數要去的位置,請注意它會使用undefined作爲分辨率值將拒絕轉換爲分辨率。由於undefined不是PersonModel,我猜這也是一個問題。