2016-10-15 11 views
0

のプロパティ「変数名」を設定することはできません私のcartCountwishlistCountが正常に例外:未定義

更新されて1

constructor(public navCtrl: NavController, 
       public productServices:Products, 
       public loadingCtrl: LoadingController) { 

     this.productServices.getCartCount(function(data){ 
     console.log("checking getCartCount" +JSON.stringify(data)); 
     this.cartCount=data.count; 
    }) 

    this.productServices.getWishlistCount(function(data){ 
     console.log("checking getWishlistCount" +JSON.stringify(data)); 
     this.wishlistCount = data.count; 

    }) 

    console.log("invoking the ionviewload " +this.cartCount); 

    console.log("invoking the ionviewload " +this.wishlistCount); 
} 

以下はい私であるように私は私のコンストラクタ内で書かれたGETリクエストを持っていますconstructor()は一度だけ実行されます。ビューが表示されたときにget要求を行う必要があります。ので、ここで私はこの

ionViewDidLoad(){ 
    console.log("invoking the Home page ionView did load "); 

    this.productServices.getCartCount(function(data){ 
     console.log("checking getCartCount" +JSON.stringify(data)); 
     this.cartCount=data.count; 
    }) 

    this.productServices.getWishlistCount(function(data){ 
     console.log("checking getWishlistCount" +JSON.stringify(data)); 
     this.wishlistCount = data.count; 

    }) 

    console.log("invoking the ionviewload " +this.cartCount); 

    console.log("invoking the ionviewload " +this.wishlistCount); 
    } 

を達成しようとするが、私は

例外としてエラーを取得しています何

:未定義

のプロパティ「cartCount」を設定することはできません、他の方法がありますいつでも私のビューが表示されたら取得要求を書く。

UPDATE: 私products.tsが

getCartCount(callbackFn: (productsArray: any) => void){ 
     this.restService.get('/getCartCount', (data)=>{ 
     console.log("invoking get CartCount details"); 
     callbackFn(data); 
     },() => { 
     callbackFn([]) 
     }) 
    } 

    getWishlistCount(callbackFn: (productsArray: any) => void){ 
     this.restService.get('/getWishlistCount', (data)=>{ 
     console.log("invoking get WishlistCount details"); 
     callbackFn(data); 
     },() => { 
     callbackFn([]) 
     }) 
    } 

ファイルをチェックし、rest.tsが

get(url:string, successCallbackFn: (data: any)=>void, errorcallbackFn:()=>void) { 
     console.log("Calling " + this.configurator.restServerBaseUrl + url); 
     var authdetails = "" 
     if (this.tokenNeeded(url)) { 
     authdetails += '&user_id=' + this.userId; 
     authdetails += '&access_token=' + this.authToken; 
     authdetails += "&" 
     } 
     this.http.get(this.configurator.restServerBaseUrl + authdetails + this.configurator.restServerControllerUrl + url).subscribe(
     result => successCallbackFn(result.json()), 
     error => errorcallbackFn()) 
    } 
+0

あなたは 'productServices'を共有していただけますか? – ranakrunal9

+0

@ ranakrunal9あなたは私のコンストラクタコードが正常に動作しているのを見ることができます。しかし私は私の意見がロードされたときに実行される要求を取得したい –

+0

'OnInit'で試してみましたか? – ranakrunal9

答えて

0

をファイルには、私はあなたのサービスの小さな変化を示唆するかもしれませんか?代わりに(維持/理解することは、ハードのコードの種類を作る)コールバックを送信するのではあなただけ(観測可能である)

rest.ts

public get(url:string) { 
     console.log("Calling " + this.configurator.restServerBaseUrl + url); 
     var authdetails = ""; 
     if (this.tokenNeeded(url)) { 
     authdetails += '&user_id=' + this.userId; 
     authdetails += '&access_token=' + this.authToken; 
     authdetails += "&" 
     } 

     let fullUrl = this.configurator.restServerBaseUrl + authdetails + this.configurator.restServerControllerUrl + url; 
     return this.http.get(fullUrl).map(res => res.json()); 
    } 

products.tsをget()を返すことができます

public getCartCount(){ 
     console.log("invoking get CartCount details"); 
     return this.restService.get('/getCartCount'); 
} 

public getWishlistCount(){ 
     console.log("invoking get WishlistCount details"); 
     this.restService.get('/getWishlistCount'); 
} 

そして、コンポーネントのコードで、あなただけの結果にsubscribeする必要があり、あなたが応答(すでにPAを取得しますrsed):この方法で物事を行うことにより

ionViewDidLoad(){ 
    console.log("invoking the Home page ionView did load "); 

    this.productServices.getCartCount().subscribe((data) => { 
     console.log("checking getCartCount" +JSON.stringify(data)); 
     this.cartCount=data.count; 
    }); 

    this.productServices.getWishlistCount().subscribe((data) => { 
     console.log("checking getWishlistCount" +JSON.stringify(data)); 
     this.wishlistCount = data.count; 
    }); 

    console.log("invoking the ionviewload " +this.cartCount); 

    console.log("invoking the ionviewload " +this.wishlistCount); 
} 

は、コンポーネントのインスタンスへthisキーワード指していることを確認することができます。

+0

私の更新された質問を確認してください –

+0

私は答えを更新しました、それは助けてくれるでしょう:) – sebaferreras

+0

あなたの提案に感謝します。 ionViewDidLoad()のget requestionコールバックの楽しみ –

関連する問題