firebase onCall函数响应返回null

时间:2018-09-16 08:07:00

标签: angular firebase google-cloud-functions nodemailer

我正在使用firebase函数创建一些无服务器函数,但是它将{data:null}返回到我的角度代码。我已经在Google上搜索了,并关注了一些帖子以将返回消息作为对象返回,但是angular仍然收到null。

以下是我的Firebase功能代码:

const functions = require('firebase-functions');
var admin = require('firebase-admin');

var serviceAccount = require('./keys/private_Key.json');

admin.initializeApp({
    credential: admin.credential.cert(serviceAccount),
    databaseURL: 'https://**DBURL**.firebaseio.com'
});

exports.sendEmail = functions.https.onCall((data, context) => {
    let transporter = nodemailer.createTransport({
        host: 'smtp.gmail.com',
        port: 465,
        secure: true, // true for 465, false for other ports
        auth: {
            user: "mygmailaccount@gmail.com",
            pass: "myGmailAccountPassword"
        }
    });

    // setup email data with unicode symbols
    let mailOptions = {
        from: '"System Message" <mygmailaccount@gmail.com>', // sender address
        to: data.recipient,
        subject: data.subject,
        text: data.text, // plain text body
        html: data.html // html body
    };

    // send mail with defined transport object
    return transporter.sendMail(mailOptions, (error, info) => {

        let log = {}
        log['req'] = mailOptions
        log['dtcreate'] = Date.now()
        log['uid'] = context.auth.uid
        log['email'] = context.auth.token.email
        log['site'] = "system email"
        log['action'] = "notification email"



        if (error) {
            console.log(error);
            log['status'] = "failed"
            log['res'] = {}
            log['res']['after'] = error
            log['res']['before'] = ''
            log['res']['msg'] = "Failed to send email: " + data.recipient
            admin.database().ref('/emailLog').push(log).then(pushData => {
                console.log(pushData)
            }, pushError => {
                console.log(pushError)
            })
            throw new functions.https.HttpsError('Failed to send email', error);
        }

        console.log('Message sent: %s', info.messageId);

        log['status'] = "success"
        log['res'] = {}
        log['res']['after'] = info
        log['res']['before'] = ''
        log['res']['msg'] = "email successfully sent to: " + data.recipient
        admin.database().ref('/emailLog').push(log).then(pushData => {
            console.log(pushData)
        }, pushError => {
            console.log(pushError)
        })

        let msg = {
            code: 1,
            msg: "email successfully sent to: " + data.recipient
        }

        return msg
    });

});

下面是我如何在Angular的服务中调用Firebase Function:

let sendEmailFunction = firebase.functions().httpsCallable('sendEmail')
          let email = {
            recipient: this.currentUser.email,
            subject: "IWO Updated: " + iwo.iwoNum,
            text: "This is to inform you that a new IWO has been created by " + this.currentUser.email,
            html: "This is to inform you that a new IWO has been created by <b>" + this.currentUser.email + "</b>"
          }
sendEmailFunction(email).then(data3 => {
            console.log(data3) //return {data:null}
          }, error => {
            console.log(error)
          })

我的问题: 1)console.log(data3)返回{data:null} 2)还请告知代码行throw new functions.https.HttpsError('Failed to send email', error);是否有效

0 个答案:

没有答案