2017-04-11 33 views
0

私は、ユーザーがリストを撮ることができるプロジェクトを作成しています。そして、プログラムはcropperjを使って画像を切り抜くことができます。それが今、私はこの2エラーTypeError:未定義の 'forEach'プロパティを読み取ることができません

EXCEPTION: Cannot read property 'forEach' of undefined

TypeError: Cannot read property 'forEach' of undefined

とコンパイラによって与えられたヒントの一つに直面していますimage.However FRMテキストを抽出しますvisionAPIにトリミングされたimageeこの

at CameraPage.getText (main.js:82525)

Iであります何が間違っているかわからない

以下は

コードです:

camera.ts

export class CameraPage { 

    public image:string; 
    width:number = 500; 
    height:number = 500; 
    quality:number = 90; 

    labels: Array<any> = []; 

    constructor(
    public navCtrl: NavController, 
    public navParams: NavParams, 
    public testService: TestService, 
    public cameraService: CameraService, 
    public toastCtrl: ToastController 
) {} 

    addPhoto(){ 
    this.cameraService.getImage(this.width,this.height,this.quality) 
     .subscribe((data) => { 
      this.image = (data); 
      //console.log(btoa(this.image)); 
      this.getVision(btoa(this.image)); 
     //console.log(this.image); 
     },(error) => { 
      // Toast errot and return DEFAULT_PHOTO from Constants 
      this.toast(error); 
     } 
    ); 
    } 

    toast(message: string) { 
    let toast = this.toastCtrl.create({ 
     message: message, 
     duration: 2500, 
     showCloseButton: false 
    }); 
    toast.present(); 
    } 


    getVision(image64: string) { 
    this.testService.getVisionLabels(image64).subscribe((sub) => {  
     this.labels = sub.responses[0].textAnnotations;  
     this.getText(); 

    }); 
    } 

    getText() { 
    this.labels.forEach((label) => { 
     let translation = {search: label.description, result: ''}; 
     console.log(label.description); 
    });  
    } 
} 

camera.html

<ion-header> 
    <ion-navbar> 
    <ion-title> 
     Camera 
    </ion-title> 
    </ion-navbar> 
</ion-header> 

<ion-content> 
    <img src={{image}} *ngIf="image" /> 
    <ion-card-content> 


    <button ion-button block outline (click)="addPhoto()">Take A Picture</button> 

    <div class="results"> 
     <div *ngFor="let label of labels"> 

     <h2>{{label.description}}</h2> 
     </div> 
    </div> 

    </ion-card-content> 
</ion-content> 
+3

私は推測 'this.labels = sub.responses [0] .textAnnotations'は、私はここに – n00dl3

答えて

2

どのようなエラーが意味することはthis.labelsgetText()方法で定義されていないということです。

あなたは問題を分析するために、2つの場所にブレークポイントを設定する必要があります。

    :チェックする this.labels.forEachライン

    物事を

  • this.labels = sub.responses[0].textAnnotationsラインで

    1. 第1ブレークポイントは、第2ブレークポイントの前にヒットする必要があります。注文が逆の場合、labelsプロパティは、使用しようとするとまだ設定されていません。
    2. 最初のブレークポイントで、sub.responses[0]を調べて、入手したものを確認します(textAnnotationsは未定義です)。
+0

[OK]を確認してください...定義されていませんそこでいくつかのアップデートがここにあります。私は、Googleデベロッパーツールでデバッガを実行しthis.labels.forEach線がthis.labelsの=のsub.responsesためながら、未定義であること[0] .textAnnonations戻り0 を実現: オブジェクト 長さ: __proto__: 配列(0)。だから問題はthis.labelsであると思います –

+0

を任意のアップデートを投稿issue.Willを見つけるためにしようとするだろう助けてくれてありがとう –

+0

ブレークポイントはどの順番でヒットしましたか? "this"は両方のブレークポイントで同じインスタンスを指していますか? – Lucero

関連する問題