如何将响应数据存储为typescript / angular2中的变量

时间:2017-06-06 04:06:21

标签: angular angular2-services

我是打字稿的新手。我在post函数后生成了一个id。现在通过使用post中生成的_id,使用我必须调用PUT函数进行播放暂停,当调用播放暂停按钮时,响应必须转到服务器。我在这里分享我的ts和API服务代码,

PUT的API代码:

    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:"test3@test.com"
_id:"59362522325a5a0786f661b3" 

1 个答案:

答案 0 :(得分:1)

根据您的代码,您为baseUrlpost调用了不同的put。所以它给了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); 
}