Amb*_*ude 5 email http node.js sendgrid angular
我想从我的应用程序(Angular)发送电子邮件。我尝试在 node.js 中使用 @sendgrid/mail,但它不起作用。
错误是:
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
Run Code Online (Sandbox Code Playgroud)
也许有人可以帮助我?
请在下面找到我的 app.js
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 1)
(node:18224) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
(node:18224) UnhandledPromiseRejectionWarning: Error: Unauthorized
at Request.http [as _callback] (C:\Users\Gauthier\tla\back\node_modules\@sendgrid\client\src\classes\client.js:124:25)
at Request.self.callback (C:\Users\Gauthier\tla\back\node_modules\request\request.js:185:22)
at emitTwo (events.js:126:13)
at Request.emit (events.js:214:7)
at Request.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1161:10)
at emitOne (events.js:116:13)
at Request.emit (events.js:211:7)
at IncomingMessage.<anonymous> (C:\Users\Gauthier\tla\back\node_modules\request\request.js:1083:12)
at Object.onceWrapper (events.js:313:30)
at emitNone (events.js:111:20)
(node:18224) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
Run Code Online (Sandbox Code Playgroud)
问题在于以下部分
SENDGRID_APY_KEY = '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';
sendgrid.setApiKey(process.env.SENDGRID_APY_KEY);
Run Code Online (Sandbox Code Playgroud)
当您启动服务器时,您需要将 sendgrid api 密钥作为 env 变量传入。上面的代码是SENDGRID_APY_KEY
作为 env 变量查找的。
要在不传入环境变量的情况下使其工作:
删除process.env
并将 SENDGRID_APY_KEY 声明为变量。
var SENDGRID_APY_KEY = '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';
sendgrid.setApiKey(SENDGRID_APY_KEY);
Run Code Online (Sandbox Code Playgroud)
将 SENDGRID_APY_KEY 作为环境变量传入:
SENDGRID_APY_KEY='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' node app.js // or however you are starting up the server
Run Code Online (Sandbox Code Playgroud)
如果您确实将密钥作为 env 变量传入,则需要从该 env 变量设置 sendgrid api,如下所示: sendgrid.setApiKey(process.env.SENDGRID_APY_KEY)
此外,您应该重置/更新您的 API 密钥(假设您发布的密钥是您当前的密钥)