Nextjs 13错误:尝试读取未定义的属性(读取'headers')
<p>在我的Nextjs中创建的post API端点的标题中遇到了问题。</p>
<p>我的端点用于表单提交,我将输入转发到我的电子邮件。我的当前代码可以发送电子邮件,并且我可以在我的电子邮件中正常接收到它,但是每次我发出请求时,它都会返回标题中的错误。</p>
<pre class="brush:php;toolbar:false;">import { NextResponse, NextRequest } from "next/server"
import nodemailer from "nodemailer"
export async function POST(request: NextRequest, response: NextResponse) {
const formData = await request.formData()
const emailValue = formData.get("email")
const messageValue = formData.get("message")
const numberValue = formData.get("phone_number")
if (!messageValue || !numberValue || !emailValue) {
return NextResponse.json({ message: "请填写所有必填字段!" }, { status: 400 })
}
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.PASSWORD,
},
tls: {
rejectUnauthorized: false,
},
})
const mailOptions = {
from: `${emailValue}`,
to: `${process.env.EMAIL}`,
subject: `来自联系我页面的消息 ${numberValue} - ${emailValue} `,
text: `${messageValue}`,
}
transporter.sendMail(mailOptions, (err, info) => {
if (err) {
return NextResponse.json({ message: `${err}` }, { status: 500 })
}
return NextResponse.json({ message: "电子邮件发送成功!" }, { status: 200 })
})
}</pre>
<p>我不确定我做错了什么。我在一个线程中读到过关于在return语句中执行NextResponse的内容,但即使这样也没有起作用。</p>
<p>我得到的错误信息:</p>
<pre class="brush:php;toolbar:false;">- 错误 TypeError: 无法读取未定义的属性(读取'headers')
at eval (webpack-internal:///(sc_server)/./node_modules/next/dist/server/future/route-modules/app-route/module.js:261:61)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)</pre></p>