【发布时间】:2020-07-26 23:24:39
【问题描述】:
我正在尝试从我的vue 应用程序调用的firebase 函数发送电子邮件。我的 firebase 函数如下所示:
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: 'xxxxx@gmail.com',
pass: 'xxxxxxxxxx'
}
});
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");
const mailOptions = {
from: `•••••••••@gmail.com`,
to: req.body.email,
subject: 'contact form message',
html: `<h1>Order Confirmation</h1>
<p>
<b>Email: </b>"hello<br>
</p>`
};
return transporter.sendMail(mailOptions, (error, data) => {
console.log("here")
if (error) {
return res.send({"data": error.toString()});
}
data = JSON.stringify(data)
return res.send({"data": `Sent! ${data}`});
});
});
我在这里删除了用户并传递了值。它们在我的代码中具有正确的值。我用来调用此函数的vue 方法如下所示:
sendEmail(){
let sendMail = firebase.functions().httpsCallable('sendMail');
sendMail(
{
email: 'xx@xx.xx.xx'
}
).then(
result => {
console.log(result.data)
}
)
}
调用此函数时,我在控制台中收到以下错误:
Uncaught (in promise) Error: Response is not valid JSON object.
at new e (index.cjs.js:58)
at t.<anonymous> (index.cjs.js:543)
at u (tslib.es6.js:100)
at Object.next (tslib.es6.js:81)
at s (tslib.es6.js:71)
任何帮助将不胜感激。谢谢
【问题讨论】:
标签: javascript firebase vue.js google-cloud-functions