【发布时间】:2020-04-05 08:28:08
【问题描述】:
我正在关注本教程https://dev.to/char_bone/using-netlify-lambda-functions-to-send-emails-from-a-gatsbyjs-site-3pnb 我已设置好所有内容,但在终端中出现以下错误。我可以从这里https://www.gatsbyjs.org/blog/2018-12-17-turning-the-static-dynamic/ 获得适用于 lambda 函数的 hello world 应用程序,这是开始第一个教程的先决条件。
RangeError [ERR_HTTP_INVALID_STATUS_CODE]: Invalid status code: undefined
这是带有表格的代码。您还可以在下面看到整个 repo。
import React from 'react'
import { HelmetDatoCms } from 'gatsby-source-datocms'
import { graphql } from 'gatsby'
import Layout from "../components/layout"
export default ({ data }) => {
const [formState, setFormState] = React.useState({
name: "",
email: "",
subject: "",
message: "",
})
const onChange = (e) => {
setFormState({...formState, [e.target.name]: e.target.value });
}
const submitForm = async (e) => {
e.preventDefault();
console.log("test");
try{
const response = await fetch("/.netlify/functions/sendmail", {
method: "POST",
body: JSON.stringify(formState),
})
if (!response.ok) {
console.log(response);
return
}
console.log("success email");
} catch(e){
console.log("error");
}
}
return(
<Layout>
<article className="sheet">
<HelmetDatoCms seo={data.datoCmsPricing.seoMetaTags} />
<section className="left-package-details">
tests
<div className="App">
<form onSubmit={submitForm}>
<label>
Name
<input
type="text"
name="name"
value={formState.name}
onChange={onChange}
/>
</label>
<label>
Email
<input
type="email"
name="email"
value={formState.email}
onChange={onChange}
/>
</label>
<label>
Subject
<input
type="textarea"
name="subject"
value={formState.subject}
onChange={onChange}
/>
</label>
<label>
message
<input
type="text"
name="message"
value={formState.message}
onChange={onChange}
/>
</label>
<button type="submit">Submit</button>
</form>
</div>
)
}
更新
现在根据 Pierre 的回答,我收到 500 错误
Request from ::ffff:127.0.0.1: POST /sendmail
Response with status 500 in 3 ms.
我想知道它是否与从本地主机 POST 相关,至少我知道它现在是 sendgrid 给我的错误。
我查看了 npm 调试部分,看到了这段代码,不知道具体放在哪里?
const {
classes: {
Mail,
},
} = require('@sendgrid/helpers');
const mail = Mail.create(data);
const body = mail.toJSON();
console.log(body);
控制台错误
Response {type: "basic", url: "http://localhost:8000/.netlify/functions/sendmail", redirected: false, status: 500, ok: false, …}
type: "basic"
url: "http://localhost:8000/.netlify/functions/sendmail"
redirected: false
status: 500
ok: false
statusText: "Internal Server Error"
headers: Headers {}
body: (...)
bodyUsed: false
__proto__: Response
第二次更新
我现在在终端收到以下错误 实际上我认为我什至不需要 cc,我认为这是说我没有 to 值,所以也许我的 env 变量 SENDGRID_TO_EMAIL 没有被传递?
Provide at least one of to, cc or bcc
现在如果我添加这样的抄送
const msg = {
to: SENDGRID_TO_EMAIL,
cc:"email@email.com",
from: email,
subject: subject ? subject : 'Contact Form Submission',
html: body,
};
然后我收到Unauthorized 消息
对于我的环境变量,我的根目录中有一个 .env 文件,其中包含以下内容
SENDGRID_API_KEY=SG.longsting
SENDGRID_TO_EMAIL=myemail@email.com
这是应该抓取环境变量的行
const { SENDGRID_API_KEY, SENDGRID_TO_EMAIL } = process.env
【问题讨论】:
-
你试过自己调试吗?如果没有,请这样做,并将您的发现连同任何猜测一起添加到帖子中。
-
我完全愿意这样做,我将如何调试它?我已经尝试了自己能想到的一切。
标签: javascript reactjs aws-lambda gatsby netlify