Next.js Discord

Discord Forum

Strange api fetching error with post and get

Unanswered
Common Redpoll posted this in #help-forum
Open in Discord
Common RedpollOP
Hello, I have a strange error when fetching the API. I have set the localhost via an env variable. However, my fetch then looks like this: http://localhost:3000/undefined/api/orders/orderPush
I have an API post call and a get call.
The get call works without problems with the env variable.
In the post call, however, it puts localhost in front of the env variable and here the env variable is also undefined.
export const sendOrderDb = async (formData: FormData) => {
    const response = await fetch(`${process.env.NEXT_URL}/api/orders/orderPush`, {
        method: 'post',
        body: formData,
    });

    return response;
};


export const allOrders = async () => {
    const data = await fetch(`${process.env.NEXT_URL}/api/orders/all`, {
        method: 'get',
    });

    return data.json();
};

Why is an almost identical fetch treated so differently?

0 Replies