Ho seguito 2 file, voglio fare in modo che la chiamata è in ordine. Ho provato la promessa di callback e, devo ammettere, non sono al 100% familiarità con chiamate asincrone.
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;
Come posso essere sicuro che quando sto chiamando fetch('www.example.com/auth', opzioni, opzioni.il corpo non è definito? Ho provato ad usare async/await e le funzioni di callback, non funziona niente per me.
Qualsiasi aiuto sarà molto apprezzato.
Grazie