firebase onCall ответ функции, возвращающий нуль - PullRequest
0 голосов
/ 16 сентября 2018

Я использую функцию firebase для создания некоторых серверных функций, но она возвращает {data:null} моему угловому коду.Я уже погуглил и следую за каким-то постом, чтобы вернуть возвращаемое сообщение как объект, но angular по-прежнему принимается как ноль.

ниже приведены мои коды функций 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
    });

});

Ниже показано, как я называю FirebaseФункция в службе Angular:

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);

...