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

标签 angular angular2-services

我是 typescript 的新手。我在 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" 

最佳答案

根据您的代码,您为postput 调用了不同的baseUrl。所以它给出了 404 错误

因此,在 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); 
}

关于angular - 如何将响应数据存储为 typescript/angular2 中的变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44381258/

相关文章:

angular - *ngIf 不工作

angular - 如何使用 Angular 2 中的依赖项扩展服务

typescript - Angular 2 : How to load data before rendering the component?

angular - 将普通变量转换为 Observable

angular - npm安装错误-未检测到Xcode或CLT版本?

angular - 简单的 ng-content 在 Angular 2 中不起作用

java - 为什么我在尝试从 angular5 向 spring-boot 发送 PUT 请求时收到未经授权的消息?

javascript - 回调中的 Node.js 发射器空数据

angular - 为什么在提交时更新对象中的值

Angular 2 使用 observable 的力量缓存 http 请求