簡體   English   中英

谷歌雲功能 - SendGrid - 400 錯誤請求

[英]Google Cloud Function - SendGrid - 400 Bad Request

我正在嘗試在我的 Google Cloud Function 中使用 SendGrid 發送電子郵件。 我在 Firebase 環境變量中有我的密鑰,所以它存在。

const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);

這是我的 GCF.ts 代碼:

代碼

const msg = {
    to: to,
    from: from,
    subject: 'Email Subject',
    content: [{
        type: "text/html",
        value: body
    }]
};

await sgMail.send(msg)
    .then(r => {
        console.log(r);
    });

當我觸發該功能並檢查我的日志時,這是我得到的錯誤:

錯誤

error:  { Error: Bad Request
    at ResponseError (node_modules/@sendgrid/mail/node_modules/@sendgrid/helpers/classes/response-error.js:19:5)
    at Request.http [as _callback] (node_modules/@sendgrid/mail/node_modules/@sendgrid/client/src/classes/client.js:124:25)
    at Request.self.callback (node_modules/@sendgrid/mail/node_modules/request/request.js:185:22)
    at emitTwo (events.js:106:13)
    at Request.emit (events.js:191:7)
    at Request.<anonymous> (node_modules/@sendgrid/mail/node_modules/request/request.js:1161:10)
    at emitOne (events.js:96:13)
    at Request.emit (events.js:188:7)
    at IncomingMessage.<anonymous> (node_modules/@sendgrid/mail/node_modules/request/request.js:1083:12)
    at IncomingMessage.g (events.js:292:16)
code: 400,
message: 'Bad Request',
response: 
{ headers: 
    { server: 'nginx',
        date: 'Sun, 16 Dec 2018 16:27:56 GMT',
        'content-type': 'application/json',
        'content-length': '402',
        connection: 'close',
        'access-control-allow-origin': 'https://sendgrid.api-docs.io',
        'access-control-allow-methods': 'POST',
        'access-control-allow-headers': 'Authorization, Content-Type, On-behalf-of, x-sg-elas-acl',
        'access-control-max-age': '600',
        'x-no-cors-reason': 'https://sendgrid.com/docs/Classroom/Basics/API/cors.html' },
    body: { errors: [Object] } } }

有沒有人熟悉這個錯誤? 它提到了 CORS,但這沒有任何意義,因為它是雲功能,而不是瀏覽器。 SendGrid API 不是很好,它主要遍歷字段名稱並且不提供示例。 感謝您提供的任何幫助!

編輯

只是為了更新問題,在我發送給自己的前端響應中,我發現了一個與 GCF 日志中的console.log(error)不同的錯誤:

"Email failed: Bad Request (400)
The from object must be provided for every email send. 
It is an object that requires the email parameter, 
but may also contain a name 
parameter.  e.g. {"email" : "example@example.com"}  or 
{"email" : "example@example.com", "name" : "Example Recipient"}.
    from.email
    http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.from"

編輯 2:解決方案

我的 from 電子郵件源來自 Firestore 中的一個文檔,我試圖獲取一個不存在的字段,因為我查詢的是錯誤的文檔,因此fromundefined 更正了它,並根據下面的建議/答案刪除了msg對象中的“內容”,一切正常。

根據文檔... msg有錯誤的元素:

const sg = require('@sendgrid/mail');
sg.setApiKey(process.env.SENDGRID_API_KEY);

const msg = {
    to: 'test@example.com',
    from: 'test@example.com',
    subject: 'Sending with SendGrid is Fun',
    text: 'and easy to do anywhere, even with Node.js',
    html: '<strong>and easy to do anywhere, even with Node.js</strong>',
};

sg.send(msg).then(() => {
    /* assume success */
})
.catch(error => {

    /* log friendly error */
    console.error(error.toString());

    /* extract error message */
    const {message, code, response} = error;

    /* extract response message */
    const {headers, body} = response;
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM