首页 > 解决方案 > 如何使用我的 svelte firebase + sendgrid 电子邮件功能修复 500 错误

问题描述

我正在开发一个带有用于收集用户电子邮件地址的输入的瘦登陆页面,这样我就可以使用 firebase + sendgrid 自动向他们发送电子邮件。

目前我收到 500 条错误消息:{"error":{"message":"Bad Request","status":"INVALID_ARGUMENT"}}

这是我已部署到 firebase 的函数定义:

import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin';
admin.initializeApp();
const db = admin.firestore();

// Sendgrid Config
import * as sgMail from '@sendgrid/mail';

const API_KEY = functions.config().sendgrid.key;
const TEMPLATE_ID = functions.config().sendgrid.template;
sgMail.setApiKey(API_KEY);

// Sends email via HTTP. Can be called from frontend code. 
export const genericEmail = functions.https.onCall(async (data) => {

    const msg = {
        to: data.email,
        from: 'ghns1133@gmail.com',
        templateId: TEMPLATE_ID,
        dynamic_template_data: {
            subject: data.subject,
            name: data.text,
        },
    };
    console.log(msg);
    await sgMail.send(msg);

    // Handle errors here

    // Response must be JSON serializable
    // return { success: true };

});

我在我的苗条应用程序中这样称呼它:

const callable = functions.httpsCallable('genericEmail');
          return callable({ text: 'Sending email with Svelte and SendGrid is fun!', subject: 'Email from Svelte', email: formInput}).then(console.log);

标签: javascriptfirebasesendgridsvelte

解决方案


推荐阅读