#Im lost can you make it more stupid for
1 messages ยท Page 1 of 1 (latest)
First i tried to use normal fetch with cache: no-cache. Now im studying that Request function but i dont understand how would Request use my URL which i want to call
import { NextResponse } from 'next/server'
export async function GET(request) {
const { searchParams } = new URL(request.url)
const id = searchParams.get('id')
const res = await fetch(https://data.mongodb-api.com/product/${id}, {
headers: {
'Content-Type': 'application/json',
'API-Key': process.env.DATA_API_KEY,
},
})
const product = await res.json()
return NextResponse.json({ product })
}
request.url - would use my URL when i use it in browser ?
i am not sure i understand your question.
Try this example code:
import { NextRequest, NextResponse } from "next/server";
export function GET(req: NextRequest) {
console.log("This is a log message from the GET method of the route.ts file");
const searchParams = req.nextUrl.search
console.log(searchParams) // just for sake of example include it
return NextResponse.json({
message: "hello world",
search: req.nextUrl.search,
});
}
im using javascript, not typescript. So in parameter should i use just "req" ?
Yeah
How can i use searchParams in brower ? In dynamic route it would be /test/xxx ?
i can see the message in console with /test
so searchParam is the query params, i.e. http://somehost.com?queryParam=value
http://localhost:3000/api/test=patrik - like this ?
that gives me error
What error?
404 Error
im trying to undestand how does it work actually
so i can modify my API
Ok, so it should be http://localhost:3000/api/test?query=test
Hm okay, how would i go about modyfing my code ?
const data = await fetch(url);
what is actually request, is it object ?
i can make it like this?
Request: {
url : myUrl,
cache: no-cache,
}
You need to do `fetch(url, {cache:"no-store"})
it's cache:'no-store' and not cache:no-cache
with function parameter GET(req) ?
Nooo.. the invocation.
inside the fetch function\
i.e. fetch(url, {cache: 'no-store'})
Request(fetch(url,{cache:"no-store"}) ? ๐
Im sorry but im tottaly lost with Backend and Server Side ๐
i have it like this now:
const data = await fetch(url, { cache: "no-store" });
Yeah, that line goes into your client component, do not place it inside your server component
Serverless fucntion aren't available at build time so they cannot be invoked by your server function during build and it will fail.
but that whole route is Server Side
Now i am confused
here is my Cron Instagram token refresh
i have it like this and i want to get logs from my console.log
im not getting logs, just status 200, that it worked
๐ That's not right! you need update your GET function parameter!!
You need to use the request object.
Like this:
async function GET(req) {
...
...
An example i used:
import { NextRequest, NextResponse } from "next/server";
export function GET(req) {
console.log("This is a log message from the GET method of the route.ts file");
return NextResponse.json({
message: "hello world",
search: req.nextUrl.search,
});
}
yes, and i was askin if i should specify REQ somehow:
like Request: {
url: myUrl,
cache: "no-store",
}
No, req param of the GET contains information about incoming request, if you use that request information nextjs will know that the reponse will vary on new request and it will not cache your functions response allowing it to allways execute.
Does that make sense?
Yeah, you need to define the req parameter in the function
i.e. function GET(req) { ... }
then everything else is correct right ? ๐
Try it,
Vercel's?
You are confused, you need to understand the code, look the if conditoin its surrounded by
Good luck. Gotta go.
thank you
๐
Everything builded, i can see console logs in console but unfortunately still not on Vercel Logs :/