【发布时间】: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