【问题标题】:Cloud Firebase Function works locally but doesnt work after deploymentCloud Firebase 功能在本地工作,但在部署后不起作用
【发布时间】:2020-12-17 01:40:02
【问题描述】:

我正在编写我的第一个云函数,该函数在本地运行良好,但在我将其部署到 Firebase 时就无法运行。我正在尝试调用一个 Cloud 函数,该函数将使用 Flutter Web 通过 HTTP 请求发送电子邮件。我在网上阅读并怀疑这可能是因为我无法兑现承诺。如何确保完成异步调用?

const nodemailer = require('nodemailer');
const cors = require('cors')({origin: true});

/**
* Here we're using Gmail to send
*/
let transporter = nodemailer.createTransport({
    service: 'gmail',
    auth: {
        user: 'dummyaccount@gmail.com',
        pass: 'dummypassword'
    }
});

exports.sendMail = functions.https.onRequest((req, res) => {
    cors(req, res, () => {

        return transporter.sendMail(req.body, (erro, info) => {
            if(erro){
                return res.send(erro.toString());
            }
            return res.send('Message send');
        });
    });
});


const admin = require('firebase-admin');
admin.initializeApp();



This is my Http Request using Dart for Flutter Web
void httpCallFunction() async {
    var url =
        'https://us-central1-fire-station-ope-blablala.cloudfunctions.net/sendMail';
    var response = await http.post(
      url,
      body: json.encode({
        "from": "Dummy Guy <Do-Not-Reply@gmail.com>",
        "to": "$_email",
        "subject": "Your Booking has been approved!!",
        "html": approvedMessageTosent,
      }),
      headers: {
        'Content-type': 'application/json',
      },
    );
    print('Response status: ${response.statusCode}');
    print('Response body: ${response.body}');
  }

【问题讨论】:

  • 您是否在云功能日志中发现任何错误?另外,你的颤振函数中的这些console.log 输出什么?
  • 我遇到了状态码 204,控制台说请求正文缺少数据

标签: javascript firebase flutter google-cloud-functions


【解决方案1】:

我经历了过山车来解决这个问题。显然,云功能日志返回状态码 204,并说我的“请求正文缺少数据”,但我可以看到我的数据正在发送。

所以我发现另一个人遇到了与我在此链接中遇到的完全相同的问题:Dart json.encode is not encoding as needed by Firebase Function

基本上我必须将我的数据包装在一个名为“数据”的键中。

{
   "data":{ 
      "to":"john@doe.com",
      "displayName":"Johnny",
      "from":"JaneDoe",
   }
}

https://firebase.google.com/docs/functions/callable-reference#request_body

【讨论】:

    猜你喜欢
    • 2018-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-07
    • 1970-01-01
    • 1970-01-01
    • 2020-07-13
    • 1970-01-01
    相关资源
    最近更新 更多