Axios发送数据请求的状态500,Next.js

dtcbnfnu  于 2023-08-04  发布在  iOS
关注(0)|答案(1)|浏览(116)

我正在尝试使用Next.js和Typescipt(没有NextAuth)创建一个认证系统。
向注册端点发送了一个带有“用户名”和“密码”的对象,并获得了500代码状态。
要提交的表格

<form className="space-y-6" onSubmit={(e) => handleSubmit(e)}>
              
              <Input id='username' label='Username' disabled={isLoading} type='text' placeholder='Enter your username' state={username} setState={setUsername}/>

              <Input id='password' label='Password' disabled={isLoading} type='password' placeholder='Enter your password' state={password} setState={setPassword}/>

              {aOption !== aOptions.LOGIN 
              ?
                <Input id='password' label='Password' disabled={isLoading} type='password' placeholder='Confirm the password' state={rePassword} setState={setRePassword}/>
              : 
                null}

              <Button disabled={isLoading} action={aOption === aOptions.LOGIN ? 'Sign In' : 'Sign Up'}/>

            </form>

字符串
在表单提交时触发的handleSubmit函数

type authType = {
    username: string,
    password: string,
  };

const handleSubmit = (e: FormEvent<HTMLFormElement>) => {
    e.preventDefault();
    setIsLoading(true);
    const data = {
      username: username,
      password: password
    };

    if(aOption === aOptions.REGISTER) {
      if(password === rePassword){
          axios.post('/api/register', data)
          .then(function (response) {
            console.log(response);
          })
          .catch(function (error) {
            console.log(error);
          });
          

      } else {
        throw new Error("The passwords don't match")
      }
    } else {

    }
  };


寄存器端点处的函数

type authType = {
    username: string,
    password: string,
  };

export async function POST(data: authType){
    console.log(data);
    //return data;
}


得到这些东西:

POST http://localhost:3000/api/register 500 (Internal Server Error)
AuthForm.tsx:50 
AxiosError {message: 'Request failed with status code 500', name: 'AxiosError', code: 'ERR_BAD_RESPONSE', config: {…}, request: XMLHttpRequest, …}
code
: 
"ERR_BAD_RESPONSE"
config
: 
{transitional: {…}, adapter: Array(2), transformRequest: Array(1), transformResponse: Array(1), timeout: 0, …}
message
: 
"Request failed with status code 500"
name
: 
"AxiosError"
request
: 
XMLHttpRequest {onreadystatechange: null, readyState: 4, timeout: 0, withCredentials: false, upload: XMLHttpRequestUpload, …}
response
: 
{data: '', status: 500, statusText: 'Internal Server Error', headers: AxiosHeaders, config: {…}, …}
stack
: 
"AxiosError: Request failed with status code 500\n    at settle (webpack-internal:///(app-pages-browser)/./node_modules/axios/lib/core/settle.js:24:12)\n    at XMLHttpRequest.onloadend (webpack-internal:///(app-pages-browser)/./node_modules/axios/lib/adapters/xhr.js:121:66)"
[[Prototype]]
: 
Error


The whole file tree
已尝试更改接受的数据类型,但仍然相同

ckx4rj1h

ckx4rj1h1#

我想是因为你没有回应。

import { NextResponse } from 'next/server'

export async function POST(request: Request) {
  const data: authType  = await request.json()
  return NextResponse.json({ data })
}

字符串
参考:路由处理程序

相关问题