Am 2 fisiere de mai jos, vreau să mă asigur că apelul este în ordine. Am încercat promisiunea și de apel invers, trebuie să recunosc, eu nu sunt 100% familiarizat cu async apeluri.
config.js:
import rolesJson from '../../roles';
class Config{
url;
rolesList;
constructor(callback){
var baseurl = 'www.example.com/env';
fetch(baseurl)
.then(response => response.json())
.then(data => {
this.url = data.url;
getAuth(data.env);
}).catch((error) => {
});
const getAuth= (env) => {
const headers = { 'Content-Type': 'application/json' };
const options = { method: 'POST', headers, body:JSON.stringify(rolesJson(env))};
console.log("THIS BODY SHOULD NOT BE UNDEFINED", options.body);
fetch('www.example.com/auth', options)
.then(response => response.json())
.then(data => {
}).catch((error) => {
});
};
}
}
module.exports = Config;
roles.js
var roleUrl = 'www.example.com/roles';
const setEnviroment = (rolesdata,env) => {
let reqBody = {
"environment": env,
"components": rolesdata
}
console.log("REQUEST BODY CREATED", reqBody);
return req;
}
const getRoles = (env) => {
fetch(roleUrl)
.then(response => response.json())
.then(roles => {
let rolesList = [];
roles.map(x => {
const roleObj = {
name: x.name,
id: x.id,
}
rolesList.push(roleObj);
})
return setEnviroment(rolesList, env);
}).catch((error) => {
});
};
module.exports = getRoles;
Cum pot fi sigur că, atunci când sunt de asteptare aduce('www.example.com/auth', opțiuni) opțiuni.corpul nu este nedefinit? Am încercat să folosească asincron/așteaptă și callback, nimic nu funcționează pentru mine.
Orice ajutor va fi foarte apreciat.
Multumesc