gpt4 book ai didi

node.js - 创建用户后使用 next.js API 发送电子邮件 ERR_HTTP_HEADERS_SENT

转载 作者:行者123 更新时间:2023-12-05 02:37:29 27 4
gpt4 key购买 nike

我目前正在使用 next.js API 创建用户,但是,我现在想使用 sendgrid 发送电子邮件。

我有这个设置,但是,我得到以下信息

event - compiled successfully in 661 ms (254 modules)
Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
at new NodeError (internal/errors.js:322:7)
at ServerResponse.setHeader (_http_outgoing.js:561:11)
at DevServer.renderError (/Users/ellisbrookes/Documents/Ellis-Developement/node_modules/next/dist/server/next-server.js:1628:17)
at DevServer.run (/Users/ellisbrookes/Documents/Ellis-Developement/node_modules/next/dist/server/dev/next-dev-server.js:431:35)
at runMicrotasks (<anonymous>)
at processTicksAndRejections (internal/process/task_queues.js:95:5)
at async DevServer.handleRequest (/Users/ellisbrookes/Documents/Ellis-Developement/node_modules/next/dist/server/next-server.js:305:20) {
code: 'ERR_HTTP_HEADERS_SENT'
}
error - Error [ERR_HTTP_HEADERS_SENT]: Cannot set headers after they are sent to the client
error - uncaughtException: Error [ERR_STREAM_WRITE_AFTER_END]: write after end

正在创建用户并正在发送电子邮件,但是,我不确定为什么会收到此错误。

这是我在 onSubmit api 调用方面的明智做法

const res = await fetch('/api/auth/register', {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data)
})

这是我在 api/auth/register 文件中的内容

import connectDB from '../../../lib/mongodb'
import bcrypt from 'bcrypt'
import User from '../../../models/user'

const mail = require('@sendgrid/mail');

mail.setApiKey(process.env.SENDGRID_API_KEY);

const handler = async (req, res) => {
// check if user exists in the database already
const emailExists = await User.findOne({ email: req.body.email })
if (emailExists) return res.status(400).send("Email already exists")

// hash password
const salt = await bcrypt.genSalt(10)
const hash = await bcrypt.hash(req.body.password, salt)

var user = new User({
firstname: req.body.firstname,
lastname: req.body.lastname,
username: req.body.username,
email: req.body.email,
password: hash
})

try {
user = await user.save();
res.send({ user: user._id })
} catch {
res.status(400).send(err)
}

// nodemailer
const message = `
First Name: ${req.body.firstname}\r\n
Last Name: ${req.body.lastname}\r\n
Username: ${req.body.username}\r\n
Email: ${req.body.email}
`;

const data = {
to: `${req.body.email}`,
from: 'Ellis Development <hello@ebrookes.dev>',
subject: `Welcome ${req.body.firstname} ${req.body.lastname} to Ellis Development`,
text: message,
html: message.replace(/\r\n/g, '<br />')
};

await mail.send(data);

res.status(200).json({ status: 'OK' });
}

export default connectDB(handler)

如前所述,正在创建用户并正在发送电子邮件,但不确定为什么会收到 ERR_HEADERS 错误。

最佳答案

在 try-catch block 中,您发送响应 res.send({ user: user._id }) 而不停止函数。该函数继续执行,您尝试发送另一个响应 res.status(200).json({ status: 'OK' });

我建议将 try-catch block 更改为:

try {
user = await user.save();
} catch {
return res.status(400).send(err)
}

如果出现错误(return 语句),这将停止执行,但如果 user.save() 成功完成,将继续执行。最后,它会在最后返回 200 {status: OK} 响应。

关于node.js - 创建用户后使用 next.js API 发送电子邮件 ERR_HTTP_HEADERS_SENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69968323/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com