Next.js Discord

Discord Forum

I get Undefined in console even though i should get a link

Unanswered
Saltwater Crocodile posted this in #help-forum
Open in Discord
Original message was deleted.

15 Replies

White-tailed Tropicbird
Did you try to console.log(response.data)?
What do you get if you do that?
You probably just need to add .then((r) => r.json()) after .post but before .then
Like this:
const handleBuyClick = () => {
    axios
      .post("http://localhost:3000/api/fruit/buy", {
        line_items: [{
          price_data: {
            currency: 'eur',
            product_data: {
              name: fruit.type,
            },
            unit_amount: fruit.price,
          },
          quantity: quantity,
        }]
      })
      .then((r) => r.json())
      .then((response) => {
        const url = response.data.url;
        // router.push(${url});
        console.log(url);
      })
      .catch((error) => {
        console.log(error.response);
      });
  };
oh
also I just noticed
this is not how you send data from routes
in your backend you have to do response.status(200).json(YourDataHere) instead of NextResponse.json()
Saltwater Crocodile
either way i get undefined
i use nextResponse because in my other route this was the only way it worked
i think the problem is this part line_items: [NextRequest.body.line_items[0]], "NextRequest"
White-tailed Tropicbird
yeah :/ change that from NextRequest to just request
Saltwater Crocodile
error TypeError: response.json is not a function :Dd
White-tailed Tropicbird
then try without that one line
Saltwater Crocodile
i need that line
i changed NextRequest to request.json but tge same error