2017-06-06 27 views
3

我是打字稿的新手。 post函數後生成了一個id。現在通過使用後期生成的_id,使用我必須調用PUT函數進行播放暫停,當播放暫停按鈕被調用時,響應必須去服務器。我在這裏分享我的TS和API服務的代碼,如何將響應數據作爲變量存儲在打字稿/ angular2中

API代碼PUT:

edit(updateId) { 
    console.log(updateId); 
    let authToken = JSON.parse(localStorage.getItem('authToken')); 
    var company_id = JSON.parse(localStorage.getItem('company_id')); 
    var queries = "?authToken=" + authToken + "&&_id="+ company_id +"&&view_id=2"; 
return this.http.put(urlBase + '/timerEntry/' + updateId ,options) 
        .map(this.extractData) 
        .catch(this.handleError); 
} 

TS碼: 這是POST功能

this.ApiService 
.addtimerEntries(new_task) 
       .subscribe(
       entry => { 
       console.log(entry) 
        this.todays.today.push(entry.data[0]); 
        this.updateId = entry.data[0]._id; 
       console.log(this.updateId); 
       this.toasterService.pop('success', 'Entry added 
       successfully'); 
       this.newTask.hide(); 
      }, 
      error => { 
      this.toasterService.pop('error', 'Something went wrong!'); 
     }); 

這是PUT功能:

playTimer() { 
    this.timerService.playTimer(); 
     this.playDiv = true; 
     console.log(this.updateId); 
     if (this.updateId){ 
    this.ApiService 
      .edit(this.updateId) 
      .subscribe(
      user => { 
       console.log(user); 
       this.ApiService 
          .getEntries(this.workEmail) 
          .subscribe(
          entries => { 
           console.log(entries); 
           this.entries = entries; 
         this.toasterService.pop('success', 'updated successfully');}, 
        error => { 
         //console.log(error); 
        }); 
      }, 
      error => { 
       this.toasterService.pop('error', 'Something went wrong!'); 
      }); 
    } 
    } 
    pauseTimer() { 
    this.timerService.pauseTimer(); 
     this.playDiv = false; 
    } 

安慰輸出:

data:Array(1) 
0:Object 
category_id:1 
client_id:15 
company_id:4 
department_id:26 
entry_type:"checkin_entry" 
project_id:7 
subcategories:Array(1) 
times:Array(1) 
workEmail:"[email protected]" 
_id:"59362522325a5a0786f661b3" 
+0

正在調用'edit f unction'? – Sravan

+0

你是否得到'console.log(this.updateId);'爲undefined – Sravan

+0

不,我已經得到了_id現在,當我按播放/暫停按鈕 – Bhrungarajni

回答

1

根據您的代碼,您爲postput調用了不同的baseUrl。所以它是從那裏你給404 error

所以,在PUT變化

return this.http.put(urlBase + '/timerEntry/' + updateId ,options)

return this.http.put(timerUrlBase + '/timerEntry/' + updateId ,options)

所以,你的服務代碼將是,

edit(updateId) { 
    console.log(updateId); 
    let authToken = JSON.parse(localStorage.getItem('authToken')); 
    var company_id = JSON.parse(localStorage.getItem('company_id')); 
    var queries = "?authToken=" + authToken + "&&_id="+ company_id +"&&view_id=2"; 
    return this.http.put(timerUrlBase + '/timerEntry/' + updateId ,options) 
    .map(this.extractData) 
    .catch(this.handleError); 
} 
相關問題