Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

208
Visualizações
Fetch data from local API Next.js

Today I came across one issue in Next.js api. I wanted to fetch data from my internal API in getStaticProps, but as documentation says you should not fetch your local API in getStaticProps and instead you should import the function. But what if I wanted to send a pageIndex to my local API and fetch the specific page. I cant do req.body.page here, so how do I get the data ?

my API looks like this

export const getData = async (page) => {
    const data = await fetch(
        `https://rickandmortyapi.com/api/character?page=${page}`
    );
    const response = await data.json();
    return response;
};

export default async function getCharactersAPI(req, res) {
    //here I would like to get the data that of the page (something like req.body.page)
    //and do something like const data = await getData(req.body.page)
    const data = await getData();
    res.status(200).json(data);
}

and my fetch code looks like this

export const getStaticProps = async pageContext => {
    //here I would like to put the page I want to fetch in the getCharactersAPI()
    //and do something like const query = await getCharactersAPI(3), meaning page number 3
    const query = await getCharactersAPI();
    const response = await query.json();

    return {
        props: { response },
    };
};
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

The pageContext contains the URL parameters, which presumably has the page number that you want to fetch?

So extract the info from the pageContent then call your query.

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda