Nextjs 13.4.6执行fetch Post请求时出现TypeError

m0rkklqb  于 2023-08-04  发布在  其他
关注(0)|答案(1)|浏览(198)

当我执行一个post请求来发送我的对象到API路由时,我得到一个头错误。

  • 错误类型错误:无法在eval(webpack-internal:/(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:277:61)读取未定义的属性(正在阅读'headers')

发布请求位于下面的客户端页面中,即我的page.js/app/register/page.js

"use client"
    import { useState } from 'react'
    import { useRouter } from 'next/navigation'
    
    export default function Register() {
    
        const router = useRouter()
        const [data, setData ] = useState({
            memberNumber: "",
            email: "",
            password: "",
            passwordVerify: ""
        })
    
        const regiserUser = async (e) => {
            e.preventDefault()
            console.log({data})
            const response = await fetch('/api/register',{method: "POST", headers: {'Content-Type': 'application/json'}, body: JSON.stringify({data})})
            const userInfo = response.json()
            console.log(userInfo)
          
          //  router.push('/login')
        }
      return (
        <>
        {/*
          This example requires updating your template:
    
          ```
          <html class="h-full bg-white">
          <body class="h-full">
          ```
        */}
        <div className="flex min-h-full flex-1 flex-col justify-center px-6 py-12 lg:px-8">
          <div className="sm:mx-auto sm:w-full sm:max-w-sm">
            <img
              className="mx-auto h-10 w-auto"
              src="https://tailwindui.com/img/logos/mark.svg?color=indigo&shade=600"
              alt="Your Company"
            />
            <h2 className="mt-10 text-center text-2xl font-bold leading-9 tracking-tight text-gray-900">
              Sign in to your account
            </h2>
          </div>
    
          <div className="mt-10 sm:mx-auto sm:w-full sm:max-w-sm">
            <form className="space-y-6" action="#" method="POST" onSubmit={regiserUser}>
            <div>
                <label htmlFor="email" className="block text-sm font-medium leading-6 text-gray-900">
                  Member Number
                </label>
                <div className="mt-2">
                  <input
                    value={data.memberNumber}
                    onChange={(e) =>{setData({...data, memberNumber: e.target.value})}}
                    id="memberNumber"
                    name="memberNumber"
                    type="text"
                    require="true"
                    className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
                  />
                </div>
              </div>
              <div>
                <label htmlFor="email" className="block text-sm font-medium leading-6 text-gray-900">
                  Email address
                </label>
                <div className="mt-2">
                  <input
                  value={data.email}
                  onChange={(e) => {setData({...data, email: e.target.value})}}
                    id="email"
                    name="email"
                    type="email"
                    require="true"
                    className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
                  />
                </div>
              </div>
    
              <div>
                <div className="flex items-center justify-between">
                  <label htmlFor="password" className="block text-sm font-medium leading-6 text-gray-900">
                    Password
                  </label>
                </div>
                <div className="mt-2">
                  <input
                  value={data.password}
                  onChange={(e) => {setData({...data, password: e.target.value})}}
                    id="password"
                    name="password"
                    type="password"
                    require="true"
                    className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
                  />
                </div>
              </div>
    
            
              <div>
                <div className="flex items-center justify-between">
                  <label htmlFor="password" className="block text-sm font-medium leading-6 text-gray-900">
                    Password Verify
                  </label>
                </div>
                <div className="mt-2">
                  <input
                  value={data.passwordVerify}
                  onChange={(e) => {setData({...data, passwordVerify: e.target.value})}}
                    id="passwordVerify"
                    name="passwordVerify"
                    type="password"
                    require="true"
                    className="block w-full rounded-md border-0 py-1.5 text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 placeholder:text-gray-400 focus:ring-2 focus:ring-inset focus:ring-indigo-600 sm:text-sm sm:leading-6"
                  />
                </div>
              </div>
    
              <div>
                <button
                  type="submit"
                  className="flex w-full justify-center rounded-md bg-indigo-600 px-3 py-1.5 text-sm font-semibold leading-6 text-white shadow-sm hover:bg-indigo-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-indigo-600"
                >
                  Sign in
                </button>
              </div>
            </form>
    
            
          </div>
        </div>
      </>

  )
}

字符串
这是我过去在next的12版中创建post fetch的方式,也是我在不同视频中从其他程序员那里看到的。我也在这里发布我的route.js。我想我可能错过了一些东西,或者只是不理解新的获取过程。唯一改变的是你必须异步并等待我所理解的提取。/app/api/register/route.js

> import bcrypt from 'bcrypt'  import { client } from
> "../../../utilites/database"
> 
> export async function POST(request) {
>     const data = await request.body.data
>     console.log(data)
> 
> }

kjthegm6

kjthegm61#

所以我发现,当使用服务器端API route.js页面时,您必须使用return,您还应该使用NextResposne,这将消除TypeError
范例

return new NextResposne(JSON.stringify({message: "user created"})

字符串

相关问题