Sendgrid-nodejs: 未处理的承诺拒绝警告:错误:请求错误

创建于 2019-04-24  ·  9评论  ·  资料来源: sendgrid/sendgrid-nodejs

得到以下错误:

(node:1475) UnhandledPromiseRejectionWarning: Error: Bad Request
    at Request.http [as _callback] (node_modules/@sendgrid/client/src/classes/client.js:124:25)
    at Request.self.callback (node_modules/request/request.js:185:22)
    at Request.emit (events.js:182:13)
    at Request.<anonymous> (node_modules/request/request.js:1161:10)
    at Request.emit (events.js:182:13)
    at IncomingMessage.<anonymous> (node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:273:13)
    at IncomingMessage.emit (events.js:187:15)
    at endReadableNT (_stream_readable.js:1094:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:1475) 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:1475) [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.

我的代码在 express 和 node 中作为 nextjs 应用程序的一部分:

const express = require('express')
const next = require('next')
const bodyParser = require('body-parser')
const sgMail = require('@sendgrid/mail');

const dev = process.env.NODE_ENV !== 'production'
const app = next({ dev })
const handle = app.getRequestHandler()

sgMail.setApiKey(myKey);

const send = ({ email, name, text }) => {
  const message = {
    to: '[email protected]',
    from: email,
    subject: 'New message from',
    text: text,
  }

  return new Promise((resolve, reject) => {
    sgMail.send(message, (error, info) =>
      error ? reject(error) : resolve(info)
    )
  })
}

app.prepare().then(() => {
  const server = express()

  server.use(bodyParser.json())

  server.get('*', (req, res) => {
    return handle(req, res)
  })

  server.post('/api/contact', (req, res) => {
    const { email, name, message } = req.body
    send(email, name, message)
    res.send('success')
  })

  server.listen(3000, (err) => {
    if (err) throw err
    console.log('> Read on http://localhost:3000')
  })
})
  • sendgrid-nodejs 版本:master(最新提交:[提交编号])
    "@sendgrid/mail": "^6.3.1",
  • Node.js 版本:v10.14.2
unknown or a waiting for feedback non-library issue

最有用的评论

@AWR14
我遇到了类似的问题,我通过确保from电子邮件字段是有效电子邮件来修复它

所有9条评论

你好@AWR14

当您不处理承诺拒绝时会发生此错误。 是一个例子。

谢谢!

最诚挚的问候,

埃尔默

@AWR14
我遇到了类似的问题,我通过确保from电子邮件字段是有效电子邮件来修复它

@hkadyanji ,(以及其他任何人)奇怪的是,我发现如果我不提供所有字段并确保它们至少有一个空格,我会收到错误 - 而不是将字段设置为“”,我设置它到“”(引号内的空格),它解决了我的问题——我觉得这很奇怪,但固定总比损坏好。 :) 如果有人有任何反馈/见解,我很想知道......

公平地说,我没有测试 to: 和 from: 允许空白的字段,因为它们通常在我的应用程序中有一个值,所以没有看到需要。 我会在安全方面进行播放,并确保任何缺失的字段至少有一个空间,直到在未来的版本中证明否则。 但话说回来,我在开玩笑吗? 我不太可能再次访问此代码,直到其他事情中断...大声笑

我正在使用“@sendgrid/mail”:“^6.4.0”

问题是否得到解决,我面临同样的错误,但在 bot 的不同上下文中。
它在机器人模拟器中运行良好,但是一旦我发布到 azure,我就会收到此错误。

BotFrameworkAdapter.processActivity():500 错误 - 错误:错误请求
(节点:17240)未处理的PromiseRejectionWarning:错误:错误:请求错误

我遇到了同样的警告,但这是我的错误,有一个错字问题,而不是text ,我输入了test

要使其再次工作,请确保电子邮件和密钥有效。

const msg = {
  to: '[email protected]',
  from: '[email protected]',
  subject: 'Hello world',
  text: 'Hello plain world!',
  html: '<p>Hello HTML world!</p>',
};

就我而言,发生此错误是因为我没有在send api 之前使用 await 导致没有捕获错误。

并且,错误原因是tocc有一些交集。

删除tocc重复的电子邮件可能会解决您的问题:)

这里有两个问题:提出错误的请求和没有正确处理承诺拒绝。 对于后者,您需要使用发送承诺结果来确定何时响应您的 API 调用或使用async/await 。 对于前者,您需要添加更多调试逻辑来确定请求是 _bad_ 的原因。 我将首先记录您尝试在send调用中传递的消息对象。

你好@AWR14

当您不处理承诺拒绝时会发生此错误。 是一个例子。

谢谢!

最诚挚的问候,

埃尔默

现在新链接是这个,实际上:https://github.com/sendgrid/sendgrid-nodejs/blob/master/docs/use-cases/success-failure-errors.md ;)

但它是一个美丽的提示,因为现在它可以了解真正的错误是什么! 谢谢!

@AWR14
我遇到了类似的问题,我通过确保from电子邮件字段是有效电子邮件来修复它

确切地说,您必须检查 sendgrid 管理员中的电子邮件状态

此页面是否有帮助?
0 / 5 - 0 等级

相关问题

danielflippance picture danielflippance  ·  4评论

nicoasp picture nicoasp  ·  3评论

amlcodes picture amlcodes  ·  4评论

Loriot-n picture Loriot-n  ·  4评论

egges picture egges  ·  3评论