1
        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/index.js:47:12)

>I am trying to write all the response data into one JSON file with Promise.all
Joe Hawkins
  • 9,803
  • 2
  • 21
  • 28
petecodes
  • 31
  • 6
  • https://stackoverflow.com/questions/52669596/promise-all-with-axios – Nuttertools Mar 16 '20 at 04:00
  • Just a simple typo. It should be `Promise.all([a(), b(), c()]).then()`. You are missing the `)` before the `.then()`. – jfriend00 Mar 16 '20 at 04:31
  • @Nuttertools my axios get requests have a different syntax. I am not able to deduce my api call into one string url. Here is how my axios get request is structured 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": "6b5ca05e55msh71f4f2" }, "params": { "timezone": "Europe/London" } – petecodes Mar 16 '20 at 16:04

2 Answers2

1

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(...).

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.
  • If any of the promises does not resolve (i.e. the HTTP request fails), .then(...) will never be invoked
Joe Hawkins
  • 9,803
  • 2
  • 21
  • 28
  • Im using Promise.all([...]).then(...) and am now getting an error 400 – petecodes Mar 16 '20 at 16:01
  • 2
    I can't help you there @petecodes. That error generally means that you made a mistake in the way you sent the request to the server. You can learn more about HTTP status 400 on Google or MDN -> https://developer.mozilla.org/en-US/docs/Web/HTTP/Status/400 – Joe Hawkins Mar 16 '20 at 16:57
0
.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.

petecodes
  • 31
  • 6