2017-03-04 27 views
0

我已經使用HTTP POST請求發送數據到後端並保存數據,還從後端返回了一個名爲receiptNo的特定元素。從服務異步數據到組件 - (從HTTP POST返回數據)

現在我在服務中收到否,但無法將其發送到正在調用該服務的組件(無法獲得從服務發送到組件的異步數據的掛起)

得到以下錯誤在訂閱存在於組分:

屬性「訂閱」不會對類型「布爾」存在。

我的組件:

import { Component, NgZone,OnInit } from '@angular/core'; 
import { NavController, NavParams } from 'ionic-angular'; 
import { Printer, PrintOptions, Toast } from 'ionic-native'; 
import {LoanService} from '../../app/services/loan.service'; 
import {CollectionService} from '../../app/services//CollectionService'; 
import { CollectpaymentsPage } from '../collection/collection.component'; 
import { Collection} from '../../../../common/classes/Collection'; 
import {Observable} from 'rxjs/Rx'; 
import 'rxjs/add/operator/map'; 



@Component({ 
    selector: 'page-receipt', 
    templateUrl: 'receipt.html' 
}) 
export class ReceiptPage { 

    base64Image: any; 
    public loan: Dictionary; 
    public mobile: any; 
    // public form: any; 
    public amount: any; 
    public collection: Collection; 
    public data; 
    public res; 

    constructor(public navCtrl: NavController,public collectionService: CollectionService, public zone: NgZone, public params: NavParams) { 
     this.loan = params.get('loan'); 
     this.mobile= params.get(this.mobile); 
     // this.form= params.get('form'); 
     this.amount= params.get('amount'); 
     this.collection = params.get('collectionData'); 


    } 


    ngOnInit() { 
     console.log("this is receipt collectionss page") 
     console.log(this.collection) 
     console.log("from receipt ts") 

     // XXX check for return value and errors 
      // this.collectionService.saveCollection(this.collection); 

      this.collectionService.saveCollection(this.collection) 
       .subscribe(result => this.data = result); 
} 
} 
interface Dictionary { 
    [ index: string ]: string 
} 

我的服務(collectionService):

public saveCollection( collection: Collection) { 
     console.log("Collections Form data ") 
     let queryParameters = new URLSearchParams(); 
     let headerParams = new Headers({ 'Content-Type': 'application/json' }); 
     var userToken = JSON.parse(localStorage.getItem('currentUser')); 
     queryParameters.set('token', userToken.token); 

     let requestOptions: RequestOptionsArgs = { 
      method: 'POST', 
      headers: headerParams, 
      search: queryParameters 
     }; 


     console.log(collection) 
     var result = collection.validate() 
     console.log(result) 

     if(! result["isValid"]){ 
      console.log(result["message"]) 
      return false; 

     } 


     const url = this.basePath + '/api/v1/collection'; 

     let headers = new Headers({ 'Content-Type': 'application/json' }); 
     this.http.post(url, collection , requestOptions) 
      .subscribe((response: Response) => { 
       console.log(response); 
       let receiptNo = response.json().receiptNo; 
       console.log("this is return valuevalue") 
       console.log(receiptNo); 
       if(receiptNo){ 
        console.log("tihis is return value"); 
        console.log(receiptNo) 
        return response.json(); 

       } 
       else{ 
        console.log("failedfaileffailefailed") 
        return undefined; 
       } 
      }); 



    } 
+0

更新您的文章與組件的代碼。 oninit方法本身不足以調試您的問題。 – Aravind

回答

1

OK,這是因爲返回認購,嘗試使用無極

public saveCollection( collection: Collection): Promise<any> { 
    // code 
    return return this.http.post(url, collection , requestOptions) 
     .toPromise() 
     .then((success: Response) => { 
      return 'success' 
     }, (fail: any) => { 
      return 'fail' 
     }) 

this.collectionService.saveCollection(this.collection) 
.then(success => console.log(success), // 'success' 
    fail => console.log(fail)) /// 'fail'` 
+0

現在訂閱上的錯誤消失了......但是新的錯誤顯示出來了 - 無法讀取屬性'訂閱'undefined –

+0

嘿,我的問題解決了.....但是你能解釋我發生了什麼嗎? –

0

已使用.subscribe()函數返回一個布爾值,但它應該是Observable

此代碼不能正常工作

this.collectionService.saveCollection(this.collection) 
      .subscribe(result => this.data = result);