Javascript 将从服务器接收的对象放入我自己的类中
我有下面的类设置 设备信息类Javascript 将从服务器接收的对象放入我自己的类中,javascript,class,object,ionic-framework,Javascript,Class,Object,Ionic Framework,我有下面的类设置 设备信息类 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 con
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;
}
}
this.getdeviceinfo().then(res => {
})
.catch(err => {});
我向服务器发送了一个请求,请求获取存储在该信息中的设备的信息。
我使用以下方法从服务器获取值
来自deviceinfo.ts类的代码
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;
}
}
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;
}
}
});
}
该方法获取信息,当我在classinfo.ts类中对来自服务器的响应进行字符串化时,我得到以下信息
{"_id":"5d7548aa1c9d4400009d0c66","id":"1","name":"Mr Meowgi's Old Person Collar","photo":"data:image/jpeg;base64,/9j and then some jiberish
这很好,但是如何将这个响应(它返回的对象)放入Deviceinfo类的实例中呢?
我曾经
但这只是未知的类型。我如何将其设置为deviceInfo类型?感谢您的帮助当我从服务器返回数据时,我喜欢将返回的数据放入对象中
const data = {
serverResponse: xhr.response,
status: 200,
success: true
}
然后我按原样返回对象
return res.send({data:data})
在前端
fetch("http://localhost:4000/api/yourEndPoint")
.then(res => res.json())
.then(myResponse => {
let dataContainer = response.data;
let returnedSuccessMessage = dataContainer.success; // output true
})
.catch(err => {// do something with err})
我使用这个库来解决这个问题。因为您知道这个对象的结构,所以可以循环使用它并手动构建js obj。如果您不知道,您可以使用递归函数遍历它。@HarunYilmaz您可以给我一个如何执行此操作的示例吗?@JDunken我将如何递归执行此操作?对于响应,您知道
Deviceinfo
的结构,因此不需要递归。您只需循环遍历返回的对象,将所需的属性分配给变量,然后使用已创建的Deviceinfo
类的构造函数。