Mailgun 在 Node.js 应用程序中看不到凭据

Mailgun not seeing credentials in Node.js app

我遇到一个问题,当我将它上传到 Heroku 时,Mailgun 没有看到 apiKey。我在我的代码顶部设置了我的环境变量和我的 dotenv,但它仍然 returns 错误。有什么我想念的吗?

require('dotenv').config();
const express = require("express");
const bodyParser = require("body-parser");
const ejs = require("ejs");
const app = express();
// MAILGUN INFO
let api_key = process.env.MAILGUN_API_KEY
let domain = process.env.DOMAIN 
var mailgun = require('mailgun-js')({apiKey: api_key, domain: domain});

//DATA PARSING AND VIEW ENGINE
app.use(express.static("public"));
app.use(bodyParser.urlencoded({extended:true}));
app.use(bodyParser.json());
app.set("view engine", 'ejs');
MAILGUN_API_KEY=******************************
DOMAIN=*********.com

2021-08-27T20:07:53.000000+00:00 app[api]: Build succeeded
2021-08-27T20:07:58.685692+00:00 heroku[web.1]: Starting process with command `node app.js`
2021-08-27T20:08:00.618937+00:00 app[web.1]: /app/node_modules/mailgun-js/lib/mailgun.js:16
2021-08-27T20:08:00.618949+00:00 app[web.1]: throw new Error('apiKey value must be defined!')
2021-08-27T20:08:00.618949+00:00 app[web.1]: ^
2021-08-27T20:08:00.618949+00:00 app[web.1]:
2021-08-27T20:08:00.618950+00:00 app[web.1]: Error: apiKey value must be defined!
2021-08-27T20:08:00.618950+00:00 app[web.1]: at new Mailgun (/app/node_modules/mailgun-js/lib/mailgun.js:16:13)
2021-08-27T20:08:00.618950+00:00 app[web.1]: at create (/app/node_modules/mailgun-js/lib/mailgun.js:239:10) 
2021-08-27T20:08:00.618950+00:00 app[web.1]: at Object.<anonymous> (/app/app.js:11:36)
2021-08-27T20:08:00.618951+00:00 app[web.1]: at Module._compile (internal/modules/cjs/loader.js:1063:30)    
2021-08-27T20:08:00.618951+00:00 app[web.1]: at Object.Module._extensions..js (internal/modules/cjs/loader.js:1092:10)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Module.load (internal/modules/cjs/loader.js:928:32)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Function.Module._load (internal/modules/cjs/loader.js:769:14)
2021-08-27T20:08:00.618952+00:00 app[web.1]: at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:72:12)
2021-08-27T20:08:00.618953+00:00 app[web.1]: at internal/main/run_main_module.js:17:47
2021-08-27T20:08:00.666412+00:00 heroku[web.1]: Process exited with status 1
2021-08-27T20:08:00.719053+00:00 heroku[web.1]: State changed from starting to crashed

您的 .env 文件只能用于本地配置。

"The .env file lets you capture all the config vars that you need in order to run your app locally." -- https://devcenter.heroku.com/articles/heroku-local

在部署到 Heroku 的服务器上时,使用 heroku_config 命令或 Heroku 的 GUI 为您的应用程序设置环境变量。可以在 Heroku's website.

上找到更多详细信息