我是 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"
最佳答案
根据您的代码,您为post
和put
调用了不同的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/