【问题标题】:unable to get data sent from function call to firebase cloud functions无法获取从函数调用发送到 Firebase 云函数的数据
【发布时间】:2020-07-27 04:31:25
【问题描述】:

我有一个名为sendMailfirebase 函数,用于发送电子邮件。我正在尝试将接收者的电子邮件地址和另一个参数传递给函数。在我的vue 应用程序中,我调用该函数如下:

sendEmail(){
            console.log(this.email)
            let sendMail = firebase.functions().httpsCallable('sendMail');
            sendMail(
                {
                    "email": this.email,
                    "superu": this.superu
                }
            ).then(
                result => {
                    console.log(result)
                }
            )
        }

我的函数index.js 看起来像:

const functions = require('firebase-functions');
const admin = require("firebase-admin")
const nodemailer = require('nodemailer');

admin.initializeApp()


//google account credentials used to send email
var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true,
    auth: {
        user: '*****@****.com',
        pass: '***********'
    }
});


exports.sendMail = functions.https.onRequest((req, res) => {
    res.header("Access-Control-Allow-Origin", "*");
    res.header("Access-Control-Allow-Headers", "Content-Type");
    res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Authorization");        

    console.log(req.body['data']);
    const mailOptions = {
        from: `•••••••••@gmail.com`,
        to: req.body['data'].email,
        subject: 'contact form message',
        html: `<h2 style="color: teal">Order Confirmation</h2>
                            <a href="https://track-acquintances.firebaseapp.com/signup/${req.body.superu}">
                               <b> Register </b>"<br>
                            </a>`
    };


    return transporter.sendMail(mailOptions, (error, data) => {
        if (error) {
            return res.status(200).json({data: error.message});
        }
        data = JSON.stringify(data)
        return res.status(200).json({data: data});
    });

});

问题是我无法访问传递的电子邮件数据并且功能失败。我在函数日志中记录了req.body['data'],我看到了{ email: 'xxx@xx.xxx.x', superu: true }。但是我尝试了req.body['data'].emailreq.body['data']['email'],它们都不起作用。在我的浏览器控制台中,我得到{data: "No recipients defined"}。任何帮助,将不胜感激。谢谢

【问题讨论】:

    标签: javascript firebase vue.js google-cloud-functions


    【解决方案1】:

    您混淆了两种类型的 Cloud Functions:

    • 您的 Cloud Function 定义为 HTTPS triggered function,这意味着您可以通过在浏览器中访问其 URL、调用 fetch 或使用 XMLHTTPRequest 来调用它。

    • 您的客户端代码尝试调用所谓的Callable Cloud Function,它是另一种类型。虽然 Callable Cloud Functions 也可以通过 HTTPS 直接调用,但它们有一个特定的有线协议可供调用。

    由于这两种类型的函数不匹配,您的客户端代码传递参数的格式与服务器处理的格式不同。

    您需要调用 HTTPS 函数,或者将 Cloud 函数转换为 Callable。后者看起来像:

    exports.sendMail = functions.https.onCall((data, context) => {
      const email = data.email;
      const superu = data.superu;
    
      ...
    });
    

    【讨论】:

    • 如何访问设置标题?
    猜你喜欢
    • 2019-01-18
    • 1970-01-01
    • 2021-01-21
    • 2018-02-08
    • 1970-01-01
    • 2019-06-01
    • 2018-02-21
    • 2018-06-09
    • 1970-01-01
    相关资源
    最近更新 更多