У меня есть следующие настройки класса.
Класс Deviceinfo
export class DeviceInfo {
_id: string; // MongoDb automatically adds this so this is just a placeholder
// tslint:disable-next-line: ban-types
id: string;
// tslint:disable-next-line: ban-types
name: String;
photo: string;
// Class contains all information on specific devices such as their ID, CurrentOwner and their ImageUrl
constructor(id2: string, devOwner: string, ph: string) {// Constructor to create an instance of the class
this.id = id2;
this.name = devOwner;
this.photo = ph;
}
}
И я отправил запрос на сервер, чтобы получить информацию об устройствах, хранящихся в этой информации.Я использую следующий метод для получения значений с сервера
Код из deviceinfo.ts class
this.getdeviceinfo().then(res => {
})
.catch(err => {});
Метод выглядит следующим образом
Метод GetDeviceinfo
async getdeviceinfo() {
return await new Promise( (resolve, reject) => {
let timesDone = 0;
// tslint:disable-next-line: no-var-keyword
const viewDevicesLink = '/devices/info/view'; // parameter: email
const xhr = new XMLHttpRequest();
// xhr.open('POST', this.AUTH_SERVER_ADDRESS + '/user/devices/view/', true);
xhr.open('POST', this.AUTH_SERVER_ADDRESS + viewDevicesLink, true);
xhr.setRequestHeader('Content-type', 'application/JSON;charset=UTF-8');
const us = new DeviceInfo('1', '', '');
xhr.send(JSON.stringify(us));
xhr.addEventListener('readystatechange', processRequest, false);
xhr.onreadystatechange = processRequest;
function processRequest(e) {
// tslint:disable-next-line: triple-equals
if (xhr.readyState == 4 && xhr.status == 200) {
// tslint:disable-next-line: triple-equals
if (timesDone == 0) {
// tslint:disable-next-line: prefer-const
const response = xhr.response;
timesDone++;
alert(JSON.parse(response));
resolve(JSON.parse(response));
}
// tslint:disable-next-line: triple-equals
} else if (xhr.readyState == 4) {
alert('server error: ' + xhr.status + ', response is: ' + xhr.responseText);
timesDone++;
return null;
}
}
});
}
Метод получает информацию, и когда я преобразую ответ от сервера i в класс classinfo.ts, я получаю следующее.
{"_id":"5d7548aa1c9d4400009d0c66","id":"1","name":"Mr Meowgi's Old Person Collar","photo":"data:image/jpeg;base64,/9j and then some jiberish
Что здорово, но как мне получить этот ответ (объект, который он возвращает) в экземпляр класса Deviceinfo?Я использовал
var device = res.
, но это просто неизвестный тип.Как бы я получить его типа устройстваInfo?Спасибо за помощь