EDITED: Tried below suggestions as well as something more, and now this is the code, I pretty much just made it all async await instead of then and catch...
makeNextAccount();
console.log("Making :" + merchantsToCreate.length + " Accounts")
async function makeNextAccount(currentIndex) {
for (const item of merchantsToCreate) {
try{
let accId = "";
var createAccBody = JSON.stringify(
{
"email":emails[item],
"firstName":"sdd",
"lastName":"dgdg"
});
var createAcc = {
method: 'post',
url: url1,
headers: awsHeaders,
data : createAccBody
};
const createdAccResult = await axios(createAcc); // wait for the request to finish
await Promise.all(createdAccResult);
console.log('Done!');
accId = createdAccResult.data.accountId;
console.log(JSON.stringify("ACCOUNT CREATED:" + accId));
} catch(err) {
console.log("ERROR Create acc:" + err );
}
try{
var initUnAuthBody = JSON.stringify(
{
"accountId": accId
});
var initUnAuth = {
method: 'post',
url: url2,
headers: awsHeaders,
data : initUnAuthBody
};
const initUnAuthResult = await axios(initUnAuth); // wait for the request to finish
console.log(JSON.stringify("Init unAuth for:" +accId+", with response:" + initUnAuthResult.status));
}catch(err) {
console.log("ERROR unautinit:" + err);
}
try{
var provisionBody = JSON.stringify(
{
"name": accId,
"active": "1"
});
var provision = {
method: 'post',
url: url3,
headers: awsHeaders,
data : provisionBody
};
const provisionResult = await axios(provision); // wait for the request to finish
console.log(JSON.stringify("Provision for:" + accId+", with response:" + provisionResult.status));
}catch(err) {
console.log("ERROR provision:" + err);
}
try{
var salesForceRecordody = JSON.stringify(
{
"email": emails[item],
"accountId": accId,
});
var salesForceRecord = {
method: 'post',
url: url4,
headers: awsHeaders,
data : salesForceRecordody
};
const salesForceRecordResult = await axios(salesForceRecord); // wait for the request to finish
console.log(JSON.stringify("SalesForce Record for:" +accId+", with response:" + salesForceRecordResult.status));
accCreatedList.push(accId); // Push to created list only when all steps are done
}catch(err) {
console.log("ERROR Salesforce:" + err);
}
}
}
And the result of this is, as you can see is returning a 400 error, but I don't know how to get the full response from the server when using async await in axios, it shows it easily when using then and catch but I can only get this simple response.
When printing the body / headers in the console everything seems fine, it's the same request I did before but it fails here for some reason...
Making :3 Accounts
ERROR Create acc:Error: Request failed with status code 400
ERROR unautinit:Error: Request failed with status code 400
ERROR provision:Error: Request failed with status code 400
ERROR Salesforce:Error: Request failed with status code 400
ERROR Create acc:Error: Request failed with status code 400
ERROR unautinit:Error: Request failed with status code 400
ERROR provision:Error: Request failed with status code 400
ERROR Salesforce:Error: Request failed with status code 400
ERROR Create acc:Error: Request failed with status code 400
ERROR unautinit:Error: Request failed with status code 400
ERROR provision:Error: Request failed with status code 400
ERROR Salesforce:Error: Request failed with status code 400