2017-09-29 68 views
-1

我正在從http請求獲取對象。我使用put方法通過另一個http請求存儲該對象。我想在存儲該對象之前使用Google翻譯API將該對象中的一些字符串翻譯爲不同的語言。我怎樣才能做到這一點。如何在從http請求獲取對象之後從對象中轉換多個字符串,然後使用轉換後的字符串存儲該對象

service.ts

import { Injectable } from '@angular/core'; 
import { HttpClient, HttpErrorResponse } from '@angular/common/http'; 
import { Observable } from 'rxjs/Rx'; 

import { BaseApi } from '../../../../laas/base-api.service'; 
import { ApplicantService } from '../../../../laas/applicant.service'; 
import { environment as ENV } from '../../../../../environments/environment' 

@Injectable() 
export class NregaService { 

    private applicantId: string; 
    private baseUrl: string; 

    constructor(
    private http: HttpClient, 
    private api: BaseApi, 
    private applicant: ApplicantService 
) { 
    this.applicantId = applicant.getApplicantID() 
    this.baseUrl = `/applicants/${this.applicantId}/id/nrega`; 

    } 

    get() { 
    return this.api.newGet(this.baseUrl); 
    } 

    save(type, data) { 
    return this.api.put(this.baseUrl, { type: type, data: data }); 
    } 

    translate(q) { 
    const translateUrl = ENV.googleAddress.translateUrl; 
    const apiKey = ENV.googleAddress.apiKey; 
    const target = 'en'; 
    const model = 'base'; 
    return this.http.post(`${translateUrl}${apiKey}`, { q, target, model }).map(res => { 
     const value = res['data'].translations[0]; 
     console.log(`Translated ${q}:`, value); 
     return value.translatedText; 
    }); 
    } 

    verify(id) { 
    return this.api.post('nrega', { jobcardid: id }).map(result => { 
     if (typeof result === typeof '') { 
     throw result; 
     } 

     const incomes = []; 
     if (result.incomeDetail) { 
     result.incomeDetail 
      .sort((a, b) => a.year <= b.year ? -1 : 1) 
      .forEach(income => incomes.push({ income: income['income(Rs.)'], year: income.year })); 
     } 

     const details = []; 
     if (result.applicantDetail) { 
     result.applicantDetail.forEach(detail => details.push({ 
      name: detail.name, 
      gender: detail.gender[0].toLowerCase(), 
      age: detail.age, 
      bankOrPostOffice: detail.bankorpostoffice, 
      aadhaarNo: detail.aadhaarNo, 
      accountNo: detail.accountNo 
     })); 
     } 

     const data = { 
     jobCardId: result.jobcardno, 
     regDate: result.dateOfRegistration, 
     photo: result.photoImageUrl, 
     voterId: result.voterId, 
     fatherOrHusband: result.nameOfFatherOrHusband, 
     family: { 
      ids: [result.familyId1, result.familyId], 
      members: details, 
      head: result.nameOfHead, 
      isBpl: result.bplFamily.toLowerCase() === 'yes' ? true : false, 
      bplId: result.bplFamilyId, 
     }, 
     address: { 
      full: result.address, 
      village: result.village, 
      district: result.district, 
      panchayat: result.panchayat, 
      block: result.block 
     }, 
     category: result.category, 
     incomes: incomes, 
     }; 
     console.log('NREGA: ', data); 

     return data; 
    }); 
    } 

} 

如果我使用也許是因爲我沒有使用.subscribe方法(我使用的部件,其中翻譯方法翻譯服務字符串本身然後我得到錯誤我在調用服務的verify()方法。在將數據返回到組件之前,是否可以將多個字符串轉換爲服務本身?

回答

0

我認爲您要查找的是Rxjs 地圖運營商。

首先,在你的service.ts文件中導入這個操作符。

import 'rxjs/add/operator/map'; 

然後,如果我得到了你的觀點,你要當你的組件調用get方法,該數據已經從服務器,但收到後翻譯一些字符串之前,該方法已發送的數據回組件。

組件> service.get()> HTTP服務器從接收到的請求>數據>翻譯送到部件

如果我是正確的,唯一的事情,你必須>數據做的是使用地圖操作:

get() { 
    return this.api.newGet(this.baseUrl) 
    .map(data => this.translate(data)); 
} 

讓我們想象一下一個SourceData類型和ModifiedData類型。地圖運算符將讓您返回Observable<ModifiedData>而不是您用於發回的Observable<SourceData>。查看文檔here以獲得更好的解釋。

請注意,您也可以在調用保存方法時進行翻譯。

save(type, data) { 
    data = this.translate(data) 
    return this.api.put(this.baseUrl, { type: type, data: data }); 
}