axios icon indicating copy to clipboard operation
axios copied to clipboard

axios.get(myimages); axios .all([requestPages, requestMyImages ]) returns undefined in nextjs

Open cadentic opened this issue 3 years ago • 0 comments

Describe the issue

we just want multiple components of strapi to be accessed by one single request the axios.get does its jobs but axios.all don't return anything. for example in a page say pages/index.js holds 50 different react componants and 50 different components need a feed from strapi of 50 strapi components it will not be agreate practice to do send 50 different reguest rather one http request would do the job better so Axios. all does this jobs but it must return the value to getstaticProp but it didn't.

Example Code

Code snippet to illustrate your question


Home.getInitialProps = async ctx => {
  try {
let Pages =
      "'http://localhost:1337/api/' + 'pages'";
   // let homeAaccorditions =
   //   "'http://localhost:1337/api/' + 'home-accorditions'";
   // let Menus =
    //  "'http://localhost:1337/api/' + 'menus'";

   // let HeroSections =
   //   "'http://localhost:1337/api/' + 'hero-sections'";
    let myimages = "'http://localhost:1337/api/pages?populate=*'";
    const requestPages = axios.get(Pages);
   // const requesthomeAaccorditions = axios.get(homeAaccorditions);
  //  const requestMenus = axios.get(Menus);
  //  const requestHeroSections = axios.get(HeroSections);
    const requestMyImages = axios.get(myimages);
    axios
      .all([requestPages, requestMyImages ]) // requesthomeAaccorditions, requestMenus, requestHeroSections])
      .then(
        axios.spread((...responses) => {
          const requestPages = responses[0];
          //  const requesthomeAaccorditions = responses[1];
          //  const requestMenus = responses[2];
          //  const requestHeroSections = responses[3];
          const requestMyImages = responses[2];

          // use/access the results
      
          // console.log(requestPages, requesthomeAaccorditions, requestMenus, requestHeroSections);
          return {
            
              requestPages,
              requestMyImages,
            
          }; //requesthomeAaccorditions, requestMenus, requestHeroSections };
        })
     
      ) 
  } catch (error) {
    return { error };
  }

Expected behavior, if applicable


    const res = await axios.get('http://localhost:1337/api/' + 'pages');
    const Pages = res.data;
    const res2 = await axios.get('http://localhost:1337/api/pages?populate=*')
    //    console.log(Pages);
    const images = res2.data;
    return { Pages, images };
    //  return { HomeAccordition };
  } catch (error) {
    return { error };
  }

this code works but the first one with one single request doesn't work.

Environment

  • Axios Version [e.g. 0.18.0] 8.18.0 or latest
  • Adapter [e.g. XHR/HTTP]
  • Browser [e.g. Chrome, Safari]
  • Browser Version [e.g. 22]
  • Node.js Version [e.g. v16.15.0]
  • OS: [e.g. iOS 12.1.0, OSX 10.13.4] windows 10 home
  • Additional Library Versions [e.g. nextjs 8.18.0

Additional context/Screenshots

Add any other context about the problem here. If applicable, add screenshots to help explain. package.json

{
  "name": "zettadaten_boiler_pallete",
  "version": "1.0.0",
  "private": false,
  "scripts": {
    "dev": "next dev -p 4845",
    "build": "next build",
    "start": "next start -p 4895",
    "lint": "next lint",
    "postbuild": "next-sitemap --config next-sitemap.config.js",
    "post-update": "echo \"codesandbox preview only, need an update\" && yarn upgrade --latest"
  },
  "dependencies": {
    "@babel/plugin-syntax-jsx": "^7.18.6",
    "@babel/preset-env": "^7.19.0",
    "@emotion/cache": "latest",
    "@emotion/react": "latest",
    "@emotion/server": "latest",
    "@emotion/styled": "latest",
    "@mui/icons-material": "latest",
    "@mui/lab": "^5.0.0-alpha.98",
    "@mui/material": "latest",
    "@mui/x-data-grid": "^5.17.1",
    "@mui/x-date-pickers": "^5.0.0",
    "@nextui-org/react": "^1.0.0-beta.10",
    "@testing-library/jest-dom": "latest",
    "@testing-library/react": "latest",
    "@testing-library/user-event": "latest",
    "@types/jest": "latest",
    "@types/node": "latest",
    "@types/react": "latest",
    "@types/react-dom": "latest",
    "autoprefixer": "latest",
    "axios": "latest",
    "dotenv": "^16.0.2",
    "moment": "^2.29.4",
    "next": "12.2.5",
    "next-redux-wrapper": "^7.0.5",
    "next-sitemap": "^3.1.22",
    "nouislider": "^15.6.1",
    "postcss": "latest",
    "prop-types": "^15.8.1",
    "react": "18.2.0",
    "react-dom": "18.2.0",
    "react-icons": "^4.4.0",
    "react-redux": "^8.0.2",
    "react-scripts": "^2.1.3",
    "redux": "^4.2.0",
    "redux-devtools-extension": "^2.13.9",
    "redux-thunk": "^2.4.1",
    "styled-components": "^5.3.5",
    "tailwindcss": "latest",
    "twin.macro": "^2.8.2",
    "typescript": "^4.8.2",
    "web-vitals": "latest"
  },
  "devDependencies": {
    "@babel/preset-react": "^7.18.6",
    "@emotion/babel-plugin": "^11.10.2",
    "eslint": "8.23.0",
    "eslint-config-next": "12.2.5"
  },
  "resolutions": {
    "styled-components": "^5"
  },
  "repository": {
    "type": "git",
    "url": "https://github.com/cadentic/strapi-webdev.git"
  },
  "keywords": [],
  "author": "[email protected]",
  "license": "ISC",
  "bugs": {
    "url": "https://github.com/cadentic/strapi-webdev/issues"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com"
  },
  "homepage": "https://github.com/cadentic/strapi-webdev",
  "babelMacros": {
    "twin": {
      "config": "./tailwind.config.js",
      "preset": "styled-components"
    }
  }
}


we just want multiple components of strapi to be accessed by one single request

cadentic avatar Sep 20 '22 19:09 cadentic