【问题标题】:async processing with nodejs and rest api calls & express - sequence error使用nodejs和rest api调用和express进行异步处理-序列错误
【发布时间】:2021-01-17 01:08:43
【问题描述】:

我正在使用 rest api 服务来访问节点运行程序,我发现 async 和 await 没有保留序列。此代码确实适用于 func 中的第二个 api 调用。我认为 await 和 async 与第二次调用的顺序不一致,这会导致问题。

我在没有第二次 api 调用的情况下使用 func 进行了测试,并且序列很好,所以问题与生成的 CCAPI 承诺有关。我认为“那么”是不正确的。此外,访问慈善 API 的 func 中的代码是独立工作的,因此实际的代码确实有效,并且也在整个代码中运行,但不按顺序运行。

任何帮助表示赞赏。

async function checkCharityAPI(searchTerm) {
    const args = { APIKey: myAPIKey, strSearch: searchTerm };
      ccAPI.GetCharitiesByName(args).then(function(result) {
        var charityResult = JSON.stringify(result);
        var charityResultJson = JSON.parse(charityResult);
        console.log(charityResultJson["GetCharitiesByNameResult"]);
        return charityResultJson;
    }).catch(function(err) {
       console.log(`Call to ${err.operationName} failed with error: ${err.err}`);
       return null;
    });

}


app.post("/api/checkcharitytest", asyncHandler(async (req, res, next) => {

        var charitySearch = req.body.charitysearch;
        console.log("start api call")
        var charityResult = await checkCharityAPI(charitySearch);
        console.log("end api call")
        res.json({ "charityResult": charityResult})

}));

加法-

我也试过这个-

var charitySearch = req.body.charitysearch;
        console.log("start api call")
//      var charityResult = await checkCharityAPI(charitySearch);
        checkCharityAPI(charitySearch).then(function(result) {
                console.log("result")
                console.log(result);
                res.json({ "charityResult": result})
        }).catch(function(err) {
           console.log(`Call to ${err.operationName} failed with error: ${err.err}`);
        });

        console.log("end api call")

我还添加了这个 - 然后通过返回值为 null 来更正 seq,尽管它已填充在 func 中 -

async function checkCharityAPI(searchTerm) {
    const args = { APIKey: myAPIKey, strSearch: searchTerm };
    await ccAPI.GetCharitiesByName(args).then(function(result) {
        var charityResult = JSON.stringify(result);
        var charityResultJson = JSON.parse(charityResult);
        console.log(charityResultJson["GetCharitiesByNameResult"]);
        return charityResultJson["GetCharitiesByNameResult"];
    }).catch(function(err) {
       console.log(`Call to ${err.operationName} failed with error: ${err.err}`);
       return null;
    });

}

【问题讨论】:

    标签: node.js api asynchronous


    【解决方案1】:

    ccAPI.GetCharitiesByName(args) 似乎返回了一个承诺,但 checkCharityAPI 没有 await

    您改为使用then/catch 访问它。

    因此,async 关键字返回的承诺完全独立于 ccAPI.GetCharitiesByName(args) 返回的承诺。

    【讨论】:

    • 我试过了,但效果是一样的(不是在 seq 中) - varcharitySearch = req.body.charitysearch; console.log("start api call") // varcharityResult = await checkCharityAPI(charitySearch); checkCharityAPI(charitySearch).then(function(result) { console.log("result") console.log(result); res.json({ "charityResult": result}) }).catch(function(err) { console .log(Call to ${err.operationName} failed with error: ${err.err}); }); console.log("结束 api 调用")
    • @TrevorLeeOakley — 这也是同样的问题。您需要更改 checkCharityAPI 函数本身。
    • 感谢您的贡献。我在被调用的func中添加了await,现在seq很好,但返回值为null。这让我很困惑,因为 seq 看起来是正确的。我在问题中添加了新代码作为编辑。
    • 那不应该返回null。它应该返回undefined,因为checkCharityAPI 没有return 语句。
    • 终于成功了。我曾假设被调用函数中的等待返回将返回到调用函数。我把它移到了最后,一切正常。
    猜你喜欢
    • 2018-03-05
    • 2017-11-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-13
    • 1970-01-01
    • 2020-04-01
    相关资源
    最近更新 更多