私はAngular9で2か月しか開発していないので、これが些細なことであれば許してください:)
基本的に、サーバーサイドに移動してオブジェクトを返すAPIを呼び出します。これはPUTリクエストです。リクエストと一緒にデータを渡し、少し何かを取り戻します。
私のサービスからのAPI呼び出し:
verify(packGuid: string, userGuid: string, pinToVerify: string, device: string)
:Observable<checkinStartedResponse> {
return this.httpClient.put<checkinStartedResponse>(`package/CheckinStartedAction/${packGuid}/${userGuid}/${pinToVerify}/${device}`, null);
import { PINValidation } from './PINValidation';
export interface checkinStartedResponse {
pinValidation: PINValidation,
isActionComplete:boolean,
pageToDisplay: string
}
PIN検証は次のとおりです。
export interface PINValidation {
attemptsRemaining: number;
canTryAgain: boolean;
isPINValid: boolean;
packageId: string;
}
これが私のサービスからそのverifyPINメソッドを呼び出している私のコンポーネントであり、私が戻したいデータを期待しています。verifyPINにカーソルを合わせると、Observable <checkinStartedResponse>を元に戻したいことがわかります。すごい。
//try to verify PIN
this.service.verifyPIN(this.packGuid, this.user.userGuid, this.pinToVerify, this.device)
.subscribe( (data) => {
console.log(data.isActionComplete);
this.pinValidationResponse = data.pinValidation;
this.pinValidationResponse.isPINValid = data.pinValidation.isPINValid;
this.checkinResponse.pinValidation.isPINValid = data.pinValidation.isPINValid;
this.passVerficiation = this.pinValidationResponse.isPINValid;
if (this.passVerficiation) {
console.log("ya");
this.PINverified.emit(true);
this.closeModal();
}
デバッグとConsole.logの使用に関する懸念事項、isActionCompleteとpageToDisplayの値を完全に読み取ることができます。画像を参照しかし、コンポーネント内のネストされたpinValidationオブジェクト内の値を読み取ろうとすると、常に未定義になります。
サーバーコードでデバッグすると、API呼び出しが処理されるのがわかります。だから、それは私が欲しいオブジェクトを返します、私はそれを読むことができません。私のクライアントサイドクラスは、サーバーサイドクラスから作成されました。繰り返しますが、2つのプロパティisActionCompleteとpageToDisplayでデータを取得しますが、このネストされたオブジェクトの値を読み取ることができません。
申し訳ありませんが、私の最初の投稿、私はこれで新しいです:(
デバッグは値を参照してください
小さな更新:PostmanでAPIをテストすると、正しい形式で期待される応答が返されます。
JSONで吐き出すこともできます。
ありがとうございました!!<3
ここに複数の問題があります。
isPINValid
!= isPinValid
。JSオブジェクトのプロパティでは、大文字と小文字が区別されます。APIからの応答にはプロパティisPinValid
がありますがisPINValid
、応答で定義されていないものを試しています。
オブジェクトの各プロパティを個別に割り当てる必要はありません。オブジェクトを割り当てると、プロパティも割り当てられます。
次を試してください
this.pinValidationResponse: checkinStartedResponse; // <-- define the type here
this.service.verifyPIN(this.packGuid, this.user.userGuid, this.pinToVerify, this.device)
.subscribe((data: checkinStartedResponse) => { // <-- expect the type
console.log(data.isActionComplete);
this.pinValidationResponse = data.pinValidation;
this.passVerficiation = data.pinValidation.isPinValid; // <-- access `isPinValid` not `isPINValid`
if (this.passVerficiation) {
console.log("ya");
this.PINverified.emit(true);
this.closeModal();
}
});
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加