【问题标题】:.env file not working even though i required it.env 文件无法正常工作,即使我需要它
【发布时间】:2020-11-17 23:13:16
【问题描述】:

我在 server.js 文件的第一行以及我正在使用 process.env.SENDGRID_API_KEY 的文件中都需要 .env。

好像.env文件中的变量无法识别,文字没有上色。

当我运行程序时,我得到“API 密钥不以“SG”开头”,但确实如此。

controller.js

require("dotenv").config();
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(process.env.SENDGRID_API_KEY);

server.js

require('dotenv').config();
const mongoose = require ('mongoose');
const express = require('express');
...

.env 文件

SENDGRID_API_KEY = 'SG.theRestOfTheAPIKey'

【问题讨论】:

  • 如果您在控制台中注销 api 密钥会发生什么? - 通常 .env 文件没有空格和引号。试试 SENDGRID_API_KEY=SG.theRestOfTheAPIKey
  • @Bergur 我未定义

标签: javascript node.js environment-variables sendgrid


【解决方案1】:

尝试删除双引号和.env 文件中变量定义之间的空格。可能是这个问题:

SENDGRID_API_KEY=SG.theRestOfTheAPIKey

【讨论】:

  • 对我不起作用,我只是将 API 密钥放在 sgMail.setApiKey 中并且它起作用了,但我现在遇到的问题是我从 sendgrid 收到 403 禁止错误
猜你喜欢
  • 1970-01-01
  • 2015-02-20
  • 2015-05-06
  • 1970-01-01
  • 2019-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多