将 API 请求与 Node 中的 Axios 链接起来.js循环 - 无法按顺序工作



已编辑:尝试了以下建议以及更多内容,现在这就是代码,我几乎只是让它全部异步等待,而不是然后和捕获。。。

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);
}
}
}

结果是,正如你所看到的,返回了一个400的错误,但我不知道在axios中使用async wait时如何从服务器获得完整的响应,在使用then和catch时它很容易显示出来,但我只能得到这个简单的响应。

当在控制台中打印正文/标题时,一切似乎都很好,这与我以前的请求相同,但由于某种原因,它在这里失败了。。。

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

使用async/await,这相对简单:

for(...) {
const result = await axios(...); // wait for the request to finish
// do something with your result
}

基本上,您需要让您的函数返回axios正在创建的Promise。很难判断样本的间距,但看起来只需在这行中添加一个return就可以了:

return axios(createAcc)

然后,您不需要使用setTimeout和循环,而是需要进行第一个调用,等待它完成,然后在then()方法中,如果您有一个调用,则进行下一个调用:

var merchantsToCreate = getFromSomewhere();
var currentIndex = 0;
function makeNextAccount() {
if (currentIndex < merchantsToCreate.length - 1) {
currentIndex++;
accountCreationFlow(merchantsToCreate[i], emails[i])
.then(makeNextAccount); // <- will make the next account
}
}
makeNextAccount(); // kick off the "loop"

我能用来解决这个问题的唯一解决方案是:

  1. 使用For。。放置在异步函数中的in循环
  2. 使用这种请求方式:const provisionResult=await axios(provision(

相关内容

  • 没有找到相关文章

最新更新