How to store response data as variables in typescript / angular2

I am new to typescript. I have an id created after the post function. Now using this _id generated in the post, using this function, I have to call the PUT function to pause the playback, when the pause button is called a response, it needs to go to the server. I am here sharing my ts service and API code,

API code for 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 code: This is a POST function

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!');
        });

      

This is for the PUT function:

 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;
    }

      

Comforting conclusion:

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" 

      

+3


source to share


1 answer


As per your code, you called different baseUrl

for post

and put

. so he gives404 error

So in put

change

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

to



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

So your service code will be,

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); 
}

      

+1


source







All Articles