2017-01-20 2 views
-1

개체 배열 병합에 문제가 있습니다. 내 목표는 하나에 3 배열을 병합하고 서버에 HTTP POST를 보냅니다.각도 2의 개체 병합

나는 CONCAT를 사용하지만, 필자가이 오류가 시도 :

예외 : ./PreviewEmailPage 클래스 PreviewEmailPage_Host의 오류 - 인라인 템플릿 : 0 : 0으로 인한 : 정의되지 않은

의 특성 'CONCAT'을 읽을 수 없습니다 다음은 내 코드입니다 : 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

가 실행되는 비동기 작업하여 this.FirstArraythen 전까지 정의되지 않은 것입니다. 당신이 Promise.all와 병렬로 그들 모두를 실행 할 수있는

: 갔지 (...)`에 의해 제안

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