【发布时间】:2020-04-12 07:07:00
【问题描述】:
我正在尝试获取响应并将其分配给 subscribe 方法中的变量,然后使用该变量来检索和使用获取的数据。以下是我的代码:
API:
public IHttpActionResult GetData(string empID)
{
empID = empID ?? " ";
try
{
using (var connection = new OracleConnection(ConfigurationManager.ConnectionStrings["Database"].ConnectionString))
{
IRepository repository = new RepositoryClass(connection);
DataCollection employee = new DataCollection();
employee.employeeData = repository.GetData(empID).ToList();
employee.CountInResponse = employee.employeeData.Count;
if (employee.employeeData != null && employee.CountInResponse > 0) {
return Ok (employee)
}
else
return Content(HttpStatusCode.NotFound, "No Employee data found for this request");
}
}
catch (Exception ex)
{
return CreateLevel3Exception(ex);
}
}
组件:
UpdateEmployee()
{
this.getEmployeeData()
if(
this.EmployeeOrigData.EmployeeID == this.newID
&& this.EmployeeOrigData.EmployeeName == this.newName
&& this.EmployeeOrigData.EmployeeContact == this.newContact
&& this.EmployeeOrigData.EmployeeStatus == this.newStatus
&& this.EmployeeOrigData.EmployeeAddress == this.newAdress
)
{
this.Message('info', 'Update invalid');
}
}
getEmployeeData() {
this.service.GetEmployeeData(this.addEmployeeID)
.subscribe((response) =>
{
this.EmployeeOrigData = response;
},
(err) =>
{
if (err == '404 - Not Found')
this.Message('info', err, 'Update Unsuccessful - Server error');
else
this.Message('error', 'Error', err);
});
}
服务:
GetEmployeeData(empID: string) {
debugger;
let params = new URLSearchParams();
params.set('empID',empID)
debugger;
return this.http.get(Url, { params: params })
.map(res => res.json().employeeData)
.catch(this.handleError);
}
这里我需要根据员工 ID 获取详细信息。我在 API 中得到了预期的响应,但在那之后,在订阅方法中我无法将它分配给变量 EmployeeOrigData。有什么问题?
【问题讨论】:
-
.map(res => res.employeeData)而不是.map(res => res.json().employeeData)。HttpClient默认会为你解析 json。 -
@Igor 如果我尝试此操作,则会收到错误消息,即“响应”类型上不存在属性“employeeData”。
-
您使用的是HttpClientModule还是过时的HttpModule?
-
@Igor HttpModule :_(
-
您应该升级到
HttpClientModule或添加它,然后在您重构或添加新代码的任何地方开始使用它。
标签: c# angular typescript