fetch
Answered
Southeastern blueberry bee posted this in #help-forum
Southeastern blueberry beeOP
How do I fetch a piece of data through server in my
page.tsxAnswered by Ray
// app/page.tsx
export default function Page() {
const res = await fetch("https://jsonplaceholder.typicode.com/todos/1")
const data = await res.json()
return <>{JSON.stringify(data,null,2)}</>
}24 Replies
@Southeastern blueberry bee How do I fetch a piece of data through server in my `page.tsx`
// app/page.tsx
export default function Page() {
const res = await fetch("https://jsonplaceholder.typicode.com/todos/1")
const data = await res.json()
return <>{JSON.stringify(data,null,2)}</>
}Answer
Southeastern blueberry beeOP
but when i do that
i get this
@Southeastern blueberry bee but when i do that
whenever you need the data to render?
@Southeastern blueberry bee i get this
Chinese Alligator
what do you get?
Southeastern blueberry beeOP
im getting it now
what
on
earth
ig it worked then
i swear it wasne tbefore
i guess i was doing something that was wrong
ty
Southeastern blueberry beeOP
this
@Chinese Alligator what do you get?
Southeastern blueberry beeOP
@Ray @Chinese Alligator
Chinese Alligator
the issue is with your api
@Chinese Alligator the issue is with your api
Southeastern blueberry beeOP
what needs to be fixed?
im using cors
Chinese Alligator
idk what that is
send here the code
Southeastern blueberry beeOP
alr
@Chinese Alligator idk what that is
Southeastern blueberry beeOP
const express = require("express");
const next = require("next");
const cors = require('cors')
const port = 8080;
const dev = "production";
const app = next({ dev, dir: '../client' });
const handle = app.getRequestHandler();
app.prepare().then(() => {
const server = express();
server.use(express.json())
server.use(cors())
server.get('/', (req, res) => {
return app.render(req, res, "/");
});
server.get('/api/v1/guildamount', async (req, res) => {
return res.send(1)
})
server.get('*', (req, res) => {
return handle(req, res);
});
server.listen(port, (err) => {
if (err) throw err;
console.log(`Ready on http://localhost:${port}`);
});
});im getting to /api/v1/guildamount
Southeastern blueberry beeOP
fixed