Next.js Discord

Discord Forum

Route.js and NextResponse

Unanswered
Knospak posted this in #help-forum
Open in Discord
My operations are working correctly, but I cannot access the messages I defined in route.js in my newsletter component.


route.js
import { NextResponse } from 'next/server'

export async function POST(request) {
  const StoryblokClient = require('storyblok-js-client')
  const Storyblok = new StoryblokClient({
    oauthToken: process.env.NEXT_PUBLIC_STORYBLOK_TOKEN,
  })
  const { email } = await request.json()
  const response = new NextResponse()
  Storyblok.post('spaces/000000/datasource_entries/', {
    datasource_entry: {
      name: email,
      value: email,
      datasource_id: "",
    },
  })
    .then(res => {
      Response.json({ message: 'Email added successfully !' }, { status: 401 })
    })
    .catch(error => {
      Response.json({ message: 'Email already exist' }, { status: 202 })
    })
  return response
}

newsletter.js
 const [email, setEmail] = useState('')

  const submitHandler = async e => {
    e.preventDefault()
    const res = await fetch('/api/newsletter', {
      method: 'POST',
      headers: {
        'Content-Type': 'application/json',
      },
      body: JSON.stringify({ email: email }),
    }).then(res => console.log(res))

    toast.success('Email added successfully !')
    setEmail('')
  }

0 Replies