TypeError:response.status 不是 POST 处的函数(webpack-internal:///(rsc)/./src/app/api/user/signup/route.js:19:25)

问题描述 投票:0回答:1
//route.js
import {connect} from "@/dbConnection/dbConnection";
import User from "@/models/userModel";
connect();
export async function POST(request, response) {
    try {
        const {username, email, password} = await request.json();
        console.log(username, email, password);
        return response.status(201).json({message: "User created successfully"});
    }catch (e){
        return response.status(500).json({message: "Error creating user",
        error: e.message
        });
    }
}

我正在构建一个 next.js 应用程序,在制定路线时出现此错误 错误=

"" TypeError: response.status is not a function at POST (webpack-internal:///(rsc)/./src/app/api/user/signup/route.js:19:25) ""

因为我仅使用 js 构建它,所以我无法在函数中定义类型。它会抛出语法错误。

我尝试定义类型但出现语法错误

javascript node.js api next.js routes
1个回答
0
投票

在应用程序路由器中,路由处理程序的第二个参数是事件而不是响应。

要发送响应,您可以使用内置

Response
API 或从
NextResponse
;
 导入 
"next/server"

//route.js
import {connect} from "@/dbConnection/dbConnection";
import User from "@/models/userModel";
connect();
export async function POST(request) {
    try {
        const {username, email, password} = await request.json();
        console.log(username, email, password);

        return Response.json({message: "User created successfully"}, { status: 201 });

    }catch (e){
        return Response.json({message: "Error creating user",
        error: e.message
        }, { status: 500 });
    }
}

或者

import { NextResponse } from "next/server"

// ...

return NextResponse.json({ message: "success" }, { status: 201 })
© www.soinside.com 2019 - 2024. All rights reserved.