我有一個帶有聯系表格的 Next js 應用程式。我正在使用 Emailjs 將電子郵件發送到我的 gmail 帳戶。這在開發中非常有效,但是一旦我部署它就停止作業并且它給了我這個訊息:
未捕獲 用戶 ID 是必需的。訪問https://dashboard.emailjs.com/admin/integration
這是我的代碼(與emailjs web 中的示例基本相同)
const [result, setResult] = useState(false);
const form = useRef();
const sendEmail = (e) => {
e.preventDefault();
emailjs
.sendForm(
process.env.EMAIL_SERVICE,
process.env.EMAIL_TEMPLATE,
form.current,
process.env.EMAIL_USER
)
.then(
(result) => {
console.log(result.text);
},
(error) => {
console.log(error.text);
}
);
e.target.reset();
setResult(true);
};
//THIS CODE IS JUST TO ERASE THE "SENT" MESSAGE AFTER 5 secs
setTimeout(() => {
setResult(false);
}, 5000);
我正在使用 .env 檔案來獲取實際值,并且正如我所說,它在開發中完美運行。
我是否缺少使其在生產中作業的東西?
uj5u.com熱心網友回復:
注銷環境變數。它們很可能在生產中未定義。 NextJS 環境變數檔案
來自 doc - inside 的示例next.config.js:
const {
PHASE_DEVELOPMENT_SERVER,
PHASE_PRODUCTION_BUILD,
} = require('next/constants')
// This uses phases as outlined here: https://nextjs.org/docs/#custom-configuration
module.exports = (phase) => {
// when started in development mode `next dev` or `npm run dev` regardless of the value of STAGING environmental variable
const isDev = phase === PHASE_DEVELOPMENT_SERVER
// when `next build` or `npm run build` is used
const isProd = phase === PHASE_PRODUCTION_BUILD && process.env.STAGING !== '1'
// when `next build` or `npm run build` is used
const isStaging =
phase === PHASE_PRODUCTION_BUILD && process.env.STAGING === '1'
console.log(`isDev:${isDev} isProd:${isProd} isStaging:${isStaging}`)
const env = {
RESTURL_SPEAKERS: (() => {
if (isDev) return 'http://localhost:4000/speakers'
if (isProd) {
return 'https://www.siliconvalley-codecamp.com/rest/speakers/ps'
}
if (isStaging) return 'http://localhost:11639'
return 'RESTURL_SPEAKERS:not (isDev,isProd && !isStaging,isProd && isStaging)'
})(),
RESTURL_SESSIONS: (() => {
if (isDev) return 'http://localhost:4000/sessions'
if (isProd) return 'https://www.siliconvalley-codecamp.com/rest/sessions'
if (isStaging) return 'http://localhost:11639'
return 'RESTURL_SESSIONS:not (isDev,isProd && !isStaging,isProd && isStaging)'
})(),
}
// next.config.js object
return {
env,
}
}
uj5u.com熱心網友回復:
我目前對 EmailJS 的鍵有類似的問題,我發現Emile 的這個答案幫助我更多地了解它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/409248.html
標籤:
