【发布时间】:2022-01-11 01:06:43
【问题描述】:
我正在使用 Svelte 和 SendGrid 制作一个联系表单。这是一个基本的 app.svelte:
<script>
import sgMail from '@sendgrid/mail';
sgMail.setApiKey(import.meta.env.VITE_SENDGRID);
function submitForm() {
const msg = {
to: 'test@example.com',
from: 'test@example.com',
subject: 'Sending with SendGrid is Fun',
text: 'and easy to do anywhere, even with Node.js',
html: '<strong>and easy to do anywhere, even with Node.js</strong>'
};
console.log('Form submitted');
sgMail.send(msg);
}
</script>
<form on:submit|preventDefault={submitForm}>
<button type="submit">Submit</button>
</form>
尽管调用了该函数(它在控制台中记录Form submitted),但用户在表单上选择submit 后,上面的代码不会发送电子邮件。当我将所有代码从 submitForm() 移到函数外时,代码会在页面加载时执行,所以我知道这不是我的 API 密钥的问题。
有什么我缺少的建议吗?
【问题讨论】:
-
尝试将
sgMail.send(msg)移到上一行,看看它是否再次记录成功消息。而且,控制台中是否显示任何错误? -
@Kalyan 当我移动
sgMail.send(msg);时,似乎没有记录Form submitted。如果有帮助,我注意到当我将const msg = { .. }移到函数之外时,我能够让控制台记录Form submitted。 -
@R Barnes 我对发送网格 API 不太熟悉,但似乎
sgMail.send()没有被执行或抛出错误。之前,当您将“console.log”放在send(msg)上方时,即使未调用send(),也会记录该消息。 -
sgMail.send(msg).then(() => {}, error => { console.error(error); if (error.response) { console.error(error.response.body) }});试试这个,以便记录来自 sendmail api 的任何错误。