#Im lost can you make it more stupid for

1 messages ยท Page 1 of 1 (latest)

frigid herald
#

Did you update your api function to use the request object?

autumn olive
#

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 ?

frigid herald
#

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,
  });
}
autumn olive
#

im using javascript, not typescript. So in parameter should i use just "req" ?

frigid herald
#

Yeah

autumn olive
#

How can i use searchParams in brower ? In dynamic route it would be /test/xxx ?

#

i can see the message in console with /test

frigid herald
autumn olive
frigid herald
#

No

#

Its the part starting with ? (question mark!)

#

i.e. /api?test=patrik

autumn olive
#

that gives me error

frigid herald
#

What error?

autumn olive
#

404 Error

#

im trying to undestand how does it work actually

#

so i can modify my API

frigid herald
#

What does your directory look like??

#

Are you using next-13?

autumn olive
#

app/api/test/route.js

#

yeah

frigid herald
autumn olive
#

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,
}

frigid herald
#

You need to do `fetch(url, {cache:"no-store"})

#

it's cache:'no-store' and not cache:no-cache

autumn olive
#

with function parameter GET(req) ?

frigid herald
#

Nooo.. the invocation.

#

inside the fetch function\

#

i.e. fetch(url, {cache: 'no-store'})

autumn olive
#

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" });

frigid herald
#

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.

autumn olive
#

but that whole route is Server Side

frigid herald
#

Now i am confused

autumn olive
#

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

frigid herald
#

๐Ÿ˜“ That's not right! you need update your GET function parameter!!

#

You need to use the request object.

autumn olive
#

thats why im asking if there should be "req"

#

and how would i modify it

frigid herald
#

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,
  });
}

autumn olive
#

yes, and i was askin if i should specify REQ somehow:

like Request: {
url: myUrl,
cache: "no-store",
}

frigid herald
#

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?

autumn olive
#

yeah

#

Just that parameter will be used even if Req isnt in code correct ?

frigid herald
#

Yeah, you need to define the req parameter in the function

#

i.e. function GET(req) { ... }

autumn olive
#

then everything else is correct right ? ๐Ÿ˜„

frigid herald
#

Try it,

autumn olive
#

In Console i received "Instagram Token Refreshed"

#

but not the other consoles

frigid herald
#

Vercel's?

autumn olive
#

like "trying to" ... etc

#

no my dev

#

im going to build it now and see on Vercel

frigid herald
#

You are confused, you need to understand the code, look the if conditoin its surrounded by

#

Good luck. Gotta go.

autumn olive
#

thank you

frigid herald
#

๐Ÿ™Œ

autumn olive
#

Everything builded, i can see console logs in console but unfortunately still not on Vercel Logs :/