简体   繁体   English

如何链接 axios 从 3 个不同的 API 端点获取请求?

[英]How can I chain axios get requests from 3 different API endpoints?

        router.get("/fixtures/generate", (_, res) => {
    Promise.all([
        // EPL BELOW
        axios({
            "method": "GET",
            "url": "https://api-football-v1.p.rapidapi.com/v2/fixtures/league/524/last/10",
            "headers": {
                "content-type": "application/octet-stream",
                "x-rapidapi-host": "api-football-v1.p.rapidapi.com",
                "x-rapidapi-key": "6b5ca05e55mshb0e3216e47a54acp1192aajsna0ef871f4f24"
            }, "params": {
                "timezone": "Europe/London"
            }
        }),
        // LA LIGA BELOW
        axios({
            "method": "GET",
            "url": "https://api-football-v1.p.rapidapi.com/v2/fixtures/league/775/last/10",
            "headers": {
                "content-type": "application/octet-stream",
                "x-rapidapi-host": "api-football-v1.p.rapidapi.com",
                "x-rapidapi-key": "6b5ca05e55mshb0e3216e47a54acp1192aajsna0ef871f4f24"
            }, "params": {
                "timezone": "Europe/London"
            }
        }),
        // CL BELOW
        axios({
            "method": "GET",
            "url": "https://api-football-v1.p.rapidapi.com/v2/fixtures/league/530/last/10",
            "headers": {
                "content-type": "application/octet-stream",
                "x-rapidapi-host": "api-football-v1.p.rapidapi.com",
                "x-rapidapi-key": "6b5ca05e55mshb0e3216e47a54acp1192aajsna0ef871f4f24"
            }, "params": {
                "timezone": "Europe/London"
            }
        })]
        .then((response) => {
            fs.writeFile(scoresFilePath, JSON.stringify(response.data.api.fixtures), err => {
                if (err) return res.status(409).send("File not saved"); // find right error code 
                return
                console.log("scores saved!");
            });
        })
        .catch(err => res.status(400).send("Could not fetch data"))
    )

});

I used square brackets to place all my calls in an array and am getting the error: TypeError: [axios(...),axios(...),axios(...)].then is not a function at /Users/Desktop/server/routes/routes.js:62:10 at Layer.handle [as handle_request] (/Users/Desktop/server/node_modules/express/lib/router/layer.js:95:5) at next (/Users/one/Desktop/server/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/Users/one/Desktop/server/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/Users//Desktop/server/node_modules/express/lib/router/layer.js:95:5) at /Users/Desktop/server/node_modules/express/lib/router/index.js:281:22 at Function.process_params (/Users/Desktop/server/node_modules/express/lib/router/index.js:335:12) at next (/Users/Desktop/server/node_modules/express/lib/router/index.js:275:10) at Function.handle (/Users/Desktop/server/node_modules/express/lib/router/index.js:174:3) at router (/Users/Desktop/server/node_modules/express/lib/router/ind我使用方括号将所有调用放在一个数组中,并收到错误:TypeError: [axios(...),axios(...),axios(...)].then is not a function at / Users/Desktop/server/routes/routes.js:62:10 at Layer.handle [as handle_request] (/Users/Desktop/server/node_modules/express/lib/router/layer.js:95:5) 在下一个 ( /Users/one/Desktop/server/node_modules/express/lib/router/route.js:137:13) 在 Route.dispatch (/Users/one/Desktop/server/node_modules/express/lib/router/route.js :112:3) 在 Layer.handle [as handle_request] (/Users//Desktop/server/node_modules/express/lib/router/layer.js:95:5) 在 /Users/Desktop/server/node_modules/express/ lib/router/index.js:281:22 at Function.process_params (/Users/Desktop/server/node_modules/express/lib/router/index.js:335:12) 在下一个 (/Users/Desktop/server/node_modules) /express/lib/router/index.js:275:10) 在 Function.handle (/Users/Desktop/server/node_modules/express/lib/router/index.js:174:3) 在路由器 (/Users/Desktop /server/node_modules/express/lib/router/ind ex.js:47:12)例如.js:47:12)

>I am trying to write all the response data into one JSON file with Promise.all

The error that you are seeing is because you are attempting to invoke .then() on an array (ex: Promise.all([...].then(...)) ), not on the result of Promise.all (ex: Promise.all([...]).then(...) .您看到的错误是因为您试图在数组上调用.then() (例如: Promise.all([...].then(...)) ),而不是Promise.all的结果(例如: Promise.all([...]).then(...)

Also remember the following:还要记住以下几点:

  • When all of the requests return, response in .then((response) => ...) will be an array with the resolved values of each of the axios calls.当所有的请求的返回, response.then((response) => ...)将与每个Axios公司呼叫的解析值的数组。
  • If any of the promises does not resolve (ie the HTTP request fails), .then(...) will never be invoked如果任何承诺没有解决(即 HTTP 请求失败), .then(...)将永远不会被调用
.then((response) => {

    response.forEach(responseData => {
        newScoresObj.matchData = [newScoresObj.matchData, 
...responseData.data.api.fixtures]
    })
    fs.writeFile(scoresFilePath, JSON.stringify(newScoresObj), err => {
        if (err) return res.status(409).send("File not saved");

Added [] correctly to place calls in array.正确添加了 [] 以在数组中放置调用。 Needed a spread operator to add each response to a temporary variable in order to then write to a JSON file.需要一个扩展运算符将每个响应添加到一个临时变量,然后写入 JSON 文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM