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

387
Visualizações
NextJS: Axios or Fetch post and get method result wrong URL path api

here I want to make a post method on API but when I call the function it posts to the wrong path of API

here is my script for the post

 //Creating Order
  const createOrder = async (data) => {
    try {
      const res = await axios.post(
        `${process.env.API_ROOT}/api/orders` ||
          `${process.env.LOCAL_URL}/api/orders`,
        data
      );
      if (res.status === 201) {
        router.push(`/api/orders/${res.data._id}`);
        dispatch(reset());
      }
    } catch (err) {
      console.log(err);
    }
  };

I call that function when approve from Paypal and using the order button, but when I press the button the URL API path change to something ${process.env.API_ROOT}/undefined/api/orders or ${process.env.LOCAL_URL}/undefined/api/orders what cause this problem ?

enter image description here

but when im using something like its working

//Creating Order
  const createOrder = async (data) => {
    try {
      const res = await axios.post(`/api/orders`,data);  <=====  its working
      if (res.status === 201) {
        router.push(`/api/orders/${res.data._id}`);
        dispatch(reset());
      }
    } catch (err) {
      console.log(err);
    }
  };

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

That is the front-end part of the code and your process.env.API_ROOT resolves to undefined. However there is a way to use process.env variables in the frontend code, you just need to prefix them with NEXT_PUBLIC like this:

process.env.NEXT_PUBLIC_LOCAL_URL
process.env.NEXT_PUBLIC_API_ROOT

More info in the official documentation

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