[英]Return Promise in Angular with typescript as custom Data Type
I am new in Typescript, and I am trying to return a promise with and object of type Device
, but I cannot reach it. 我是Typescript中的新手,我正在尝试返回类型为
Device
对象和对象,但我无法达到它。
The problem is when I return a Mock everything works good, but when I connect to a real API I have problems here response.json().data as Device
. 问题是,当我返回Mock时,一切正常,但是当我连接到真正的API时,我在这里遇到问题,
response.json().data as Device
。
When I ask for the data in the server and then I try to print this in the template, everything its gone and in the console I have Undefined
. 当我在服务器中询问数据然后我尝试在模板中打印它时,一切都消失了,在控制台中我有
Undefined
。 Also the console.log
that is in device.component.ts
in the function getDevices()
print like the old status of the object, because when I change the data appear the data that was supposed to show before. 函数
getDevices()
中的device.component.ts
中的console.log
也像对象的旧状态一样打印,因为当我更改数据时会显示之前应该显示的数据。
I have a few questions like: 我有几个问题,如:
How can I access to the response.json().data
in the .then
of the promise to see what's the structure or what's the data that it has ? 如何访问promise中的
.then
中的response.json().data
,看看它的结构是什么或者它有什么数据?
How can I map (I think this terminology is correct for Observable
, but I don't know how to say it for Promise
s) my device
object with the data of the API ? 我如何映射(我认为这个术语对于
Observable
是正确的,但我不知道如何用Promise
)我的device
对象与API的数据?
device.component.ts device.component.ts
import { Component, OnInit, Input } from '@angular/core';
import { Device } from './device';
import { DeviceService } from './device.service';
@Component({
selector: 'app-device',
templateUrl: './device.component.html'
})
export class DeviceComponent implements OnInit {
@Input() private device: Device;
constructor(private deviceService: DeviceService) {};
ngOnInit(): void {
// this.getDevice(40072);
this.deviceService.getDeviceMock().then(device => this.device = device);
}
getDevice(id: number): void {
this.deviceService.getDevice(id).then(device => this.device = device);
console.log(this.device);
// this.deviceService.getDeviceMock().then(device => this.device = device);
}
search(id: number): void {
this.getDevice(id);
}
save(): void {
this.deviceService.setDevice(this.device);
}
}
device.component.html device.component.html
<input [(ngModel)]="idSearch" type="text" placeholder="Insert ID" >
<button (click)="search(idSearch)">Search</button>
<div *ngIf="device">
<div>
<label>Uid: </label>
<input [(ngModel)]="device.Uid" placeholder="Uid">
</div>
<div>
<label>VigilId: </label>
<input [(ngModel)]="device.VigilId" placeholder="VigilId">
</div>
<div>
<label>CmfPhoneNumber: </label>
<input [(ngModel)]="device.Model.RuntimeSettings.CmfPhoneNumber" placeholder="CmfPhoneNumber">
</div>
<div>
<label>ReportInterval: </label>
<input [(ngModel)]="device.Model.RuntimeSettings.ReportInterval" placeholder="ReportInterval">
</div>
<div>
<label>GeoLocationHighAccuracy: </label>
<input [(ngModel)]="device.Model.RuntimeSettings.GeoLocationHighAccuracy" placeholder="GeoLocationHighAccuracy">
</div>
<div>
<label>AlarmCancelTimeout: </label>
<input [(ngModel)]="device.Model.RuntimeSettings.AlarmCancelTimeout" placeholder="AlarmCancelTimeout">
</div>
<div>
<label>AdherenceCheckInterval: </label>
<input [(ngModel)]="device.Model.RuntimeSettings.AdherenceCheckInterval" placeholder="AdherenceCheckInterval">
</div>
<div>
<label>PreAlarmPeriod: </label>
<input [(ngModel)]="device.Model.RuntimeSettings.PreAlarmPeriod" placeholder="PreAlarmPeriod">
</div>
<div>
<label>PingInterval: </label>
<input [(ngModel)]="device.Model.RuntimeSettings.PingInterval" placeholder="PingInterval">
</div>
<button (click)="save()">Send</button>
device.service.ts device.service.ts
import { Component } from '@angular/core';
import { Device } from './device';
import { Injectable } from '@angular/core';
import { Headers, Http } from '@angular/http';
import 'rxjs/add/operator/toPromise';
import { DeviceMock } from './device-mock'
@Injectable()
export class DeviceService {
// TODO: Create configuration file.
private apiUrl = 'https://api.com/api2/v2';
private headers = new Headers({'Authorization': 'xxxx'});
constructor(private http: Http) {};
getDeviceMock(): Promise<Device> {
return Promise.resolve(DeviceMock)
}
getDevice(id: number): Promise<Device> {
const url = `${this.apiUrl}/device/${id}?names=RuntimeSettings`;
return this.http.get(url, {headers: this.headers})
.toPromise()
.then(response => response.json().data as Device)
.catch(this.handleError);
}
setDevice(device: Device): Promise<Device> {
this.headers.set('Content-Type', 'application/json');
const url = `${this.apiUrl}/device/${device.VigilId}?names=RuntimeSettings`;
return this.http.put(url, JSON.stringify(device), {headers: this.headers})
.toPromise()
.then(response => response.json().data as Device)
.catch(this.handleError);
}
private handleError(error: any): Promise<any> {
console.error('An error occurred', error);
return Promise.reject(error.message || error);
}
};
device.ts device.ts
export interface Device {
VigilId: number;
Uid: string;
Model: Model;
};
interface Model {
RuntimeSettings: RuntimeSettings;
};
interface RuntimeSettings {
ReportInterval: number;
PingInterval: number;
PreAlarmPeriod: number;
AdherenceCheckInterval: number;
AlarmClearTimeout: number;
AlarmCancelTimeout: number;
DailyReportInterval: number;
GeoLocationRetryCount: number;
GeoLocationHighAccuracy: true;
GeoLocationTimeOut: number;
GeoMaxAgeTimeOut: number;
CmfPhoneNumber: number;
PalmTouchTrigger: boolean;
TouchTriggerCooldownPeriod: number;
DemoMode: boolean;
DeviceName: string;
VerboseLogging: boolean;
};
And this is a response of the API 这是API的回应
{
"VigilId": 41,
"Uid": "Identi",
"Model": {
"RuntimeSettings": {
"ReportInterval": 900,
"PingInterval": 300,
"PreAlarmPeriod": 10,
"AdherenceCheckInterval": 3600,
"AlarmClearTimeout": 600,
"AlarmCancelTimeout": 15,
"DailyReportInterval": 43200,
"GeoLocationRetryCount": 3,
"GeoLocationHighAccuracy": true,
"GeoLocationTimeOut": 5000,
"GeoMaxAgeTimeOut": 60,
"CmfPhoneNumber": "",
"PalmTouchTrigger": true,
"TouchTriggerCooldownPeriod": 30,
"DemoMode": false,
"DeviceName": "",
"VerboseLogging": false
}
}
}
For question (1): 对于问题(1):
You must notice that HTTP Client requests are asynchronous. 您必须注意到HTTP客户端请求是异步的。 It means the outer/parent function does not hold to wait for the network request to complete, ie, does not block .
这意味着外部/父功能不等待网络请求完成,即不阻止 。 That is the reason you get "old data" on your
getDevices
: it prints the object before updating it from server response. 这就是你在
getDevices
上得到“旧数据”的getDevices
:它在从服务器响应更新之前打印对象。
getDevice(id: number): void {
this.deviceService.getDevice(id)
.then(device => this.device = device); // network request is scheduled
console.log(this.device); // device is printed to log but not necessarily
// after the network request returns
}
To solve it, change it to: 要解决它,请将其更改为:
getDevice(id: number): void {
this.deviceService.getDevice(id).then(device => {
console.log(device); // print to console what is returned from network
this.device = device;
});
}
For question (2): 对于问题(2):
What you are doing this.device = device
is correct, that would update your this.device
object and point it to the new device
object from the server response. 你正在做什么
this.device = device
是正确的,它将更新你的this.device
对象并从服务器响应指向新的device
对象。 (Unless I understood wrongly this question). (除非我错误地理解了这个问题)。
For your second question, this line: 对于你的第二个问题,这一行:
response.json().data
appears to be looking for a property of the JSON called "data", but I don't see that anywhere in your example. 似乎在寻找名为“data”的JSON属性,但我在你的例子中没有看到任何地方。 Does the returned JSON have a property called data?
返回的JSON是否有一个名为data的属性?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.