class ApiHandler {
constructor(path, params = {}, token = null) {
this.params = params;
this.path = server + path;
this.setHeader();
}
setHeader(token = null) {
this.headers = {
'Content-Type': 'application/json'
}
if (token) {
this.header.push( {"Authorization": token} );
}
}
get() {
this.sendRequest("GET");
return this;
}
post() {
this.sendRequest("POST");
return this;
}
destroy() {
this.sendRequest("DELETE");
return this;
}
async sendRequest(verb) {
let r = null;
await fetch(this.path, {
method: verb,
mode: "cors",
headers: this.headers,
body: JSON.stringify(this.params)
})
.then(function(response) {
r = response;
});
this.response = r;
}
}
Вот класс, который отвечает за доступ к api
loginSubmit(id, password) {
let params = {id: id, password: password};
let r = new ApiHandler("/auth/login", params);
r.post();
console.log("Дампаю экземпляр:");
console.log(r); //свойство на месте
console.log("Читаю свойство: " + r.response) //undefined
}
В методе создается экземпляр класса и делается запрос. Вот только свойство response оказывается undefined при чтении, хотя при дампе самого экземпляра свойство на месте
