2017-01-20 12 views
-1

3つのオブジェクトの配列をマージするのに問題があります。私の目的は、3つの配列を1つにマージし、サーバーにHTTP POSTを送信することです。角度2のオブジェクトを結合する

私は連結を使用して試してみましたが、イムは、このエラーを取得:

例外:./PreviewEmailPageクラスPreviewEmailPage_Hostでエラーが発生しました - インラインテンプレート:0:0によって引き起こさ:未定義

のプロパティを読み取ることができません「連結」これらは私のコードです:this.localStorage.get以来

import { Component } from '@angular/core'; 
import { NavParams, NavController, LoadingController } from 'ionic-angular'; 
import { Validators, FormGroup, FormControl } from '@angular/forms'; 

import { Http, Headers} from '@angular/http'; 
import 'rxjs/add/operator/map'; 
import { Storage } from '@ionic/storage'; 


@Component({ 
    selector: 'preview-email-page', 
    templateUrl: 'preview-email.html', 
}) 
export class PreviewEmailPage { 
    headers: Headers; 
    loading: any; 
    url: string; 
    preview: any[]; 

    FirstArray: any[]; 
    SecondArray: any[]; 
    ThirdArray: any[]; 
    PostData: any[]; 

    constructor(
    public nav: NavController, 
    public navParams: NavParams, 
    public loadingCtrl: LoadingController, 
    public localStorage: Storage, 
    public http: Http, 
) { 
    this.localStorage.get('FirstArray').then((value) => { 
     this.FirstArray= value; 
    }) 
    this.localStorage.get('SecondArray').then((value) => { 
     this.SecondArray= value; 
    }) 
    this.localStorage.get('ThirdArray').then((value) => { 
     this.ThirdArray= value; 
    }) 

    this.PostData = this.FirstArray.concat(this.SecondArray); 
    this.PostData = this.PostData.concat(this.ThirdArray); 

    this.loading = this.loadingCtrl.create(); 
    } 

    ionViewWillEnter(){ 
    this.headers = new Headers(); 
    this.headers.append("Content-Type", "application/x-www-form-urlencoded"); 
    console.log(this.PostData); 
    this.getPreview(); 
    } 

    getPreview(){ 
    this.loading.present(); 
    this.url = 'https://domain.com/REST/getpreview.php'; 
    this.http.post(this.url, this.PostData, {headers: this.headers}).map(res => res.json()).subscribe(res => { 
     console.log(res); 
     this.preview = res; 
    }, err => { 
     console.log('error'); 
    }) 
    } 

} 
+0

その後、(...)'ローカルストレージからあなたのフェッチが起こっています*。 *非同期**。したがって、連結に達する頃には、これらのフィールドはまだ設定されていません。 – jonrsharpe

答えて

1

thenが実行される刚性あなたthis.FirstArrayが定義されていない非同期操作です。あなたはPromise.allと並行して、それらすべてを実行している何ができるか

:に.get(...) `によって示唆されるように

Promise.all([this.localStorage.get('FirstArray'), this.localStorage.get('SecondArray'), this.localStorage.get('ThirdArray')]).then(values => { 
    this.FirstArray= values[0]; 
    this.SecondArray= values[1]; 
    this.ThirdArray= values[2]; 
    this.PostData = this.FirstArray.concat(this.SecondArray); 
    this.PostData = this.PostData.concat(this.ThirdArray); 
}); 
+0

答えをありがとうが、今このエラーがあります:例外:未知(約束):TypeError:未定義のプロパティ 'concat'を読み取ることができません –

+0

@RedzwanLatifコールバックが目的の値を返していますか?彼らが返すものを見るためにコールバックの中にconsole.logを入れてください。 – echonax

関連する問題