Next.js Discord

Discord Forum

Help, How Can I Re-Fetch Data?

Unanswered
Cinnamon Hummingbird posted this in #help-forum
Open in Discord
Cinnamon HummingbirdOP
The first time I ran this code it worked but now it just shows the data that I got when I first ran the code & it doesn't call the API endpoint again. Pls help

I only have 1 file in the next js application because I just want to test this.
In the first function I return a Bearer token.
In the second function I call the API endpoint & return JSON data.
In the third function I display the data.

`` const getData = async () =>{ const token = await getToken(); console.log(Loaded Token ID: ${token}); if (!token) { return NextResponse.json({ error: 'Failed to authenticate' }, { status: 401 }); } try { const response = await fetch(https://api.bol.com/retailer/shipments/invoices/requests`, {
method: 'GET',
headers: {
Authorization: Bearer ${token},
'Content-Type': 'application/vnd.retailer.v10+json;charset=UTF-8',
'Accept': 'application/vnd.retailer.V10+json'
}
});

if(!response.ok){
throw new Error("Failed to fetch API data");
}

const data = await response.json();
return data;

} catch (error) {
console.error('Error fetching sales data:', error);
return NextResponse.json({ error: 'Failed to fetch sales data' }, { status: 500 });
}
}

const Login = async () => {
const apiData = await getData();
return (
#Unknown Channel
<h2>Login page !!</h2>
<div>
{JSON.stringify(apiData)}
</div>
</>
)
}

export default Login;

2 Replies

American Crow
const response = await fetch("https://api.bol.com/retailer/shipments/invoices/requests", {
            method: 'GET',
            cache: 'no-store', // cached by default https://nextjs.org/docs/app/api-reference/functions/fetch
            headers: {
                Authorization: Bearer ${token},
                'Content-Type': 'application/vnd.retailer.v10+json;charset=UTF-8',
                'Accept': 'application/vnd.retailer.V10+json'
            }
})