javascript - unable to get data sent from function call to firebase cloud functions
问题描述
I have a firebase
function called sendMail
that is used to send emails. I am trying to pass the email address of the receiver and another parameter to the function. In my vue
app I call the function as follows:
sendEmail(){
console.log(this.email)
let sendMail = firebase.functions().httpsCallable('sendMail');
sendMail(
{
"email": this.email,
"superu": this.superu
}
).then(
result => {
console.log(result)
}
)
}
And my function index.js
looks like:
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});
});
});
The problem is I can't access the passed email data and the function fails. I logged req.body['data']
to the functions logs and I see { email: 'xxx@xx.xxx.x', superu: true }
. But I tried both req.body['data'].email
and req.body['data']['email']
and they both doesn't work. And in my browsers console I get {data: "No recipients defined"}
. Any help would be appreciated. Thank you
解决方案
You're confusing two types of Cloud Functions:
Your Cloud Function is defined as an HTTPS triggered function, which means that you can invoke it by accessing its URL in a browser, by calling
fetch
, or by usingXMLHTTPRequest
.Your client code, tries to invoke a so-called Callable Cloud Function, which is a different type. While Callable Cloud Functions are also invoked directly over HTTPS, they have a specific wire protocol for being invoked.
Since the two types of function don't match, your client code is passing the parameters in a different format than what the server is handling.
You'll need to either call the HTTPS function, or convert the Cloud Function to be Callable. The latter would look something like:
exports.sendMail = functions.https.onCall((data, context) => {
const email = data.email;
const superu = data.superu;
...
});
推荐阅读
- vba - 在从 Outlook 的资源管理器窗格中删除邮件之前,Outlook 会收到警告
- android - 如何像在桌面上的主要 ide 中一样在颤动的文本字段中实现自动保存?
- android - Android Facebook 登录 - 应用已禁用 - URL 合规性
- android - 如何在android中的外部存储卡上写入
- python - 如何在使用 python 检查是否存在时在 sqlite3 表中插入数据
- wordpress - 如何删除转到 wordpress 的旧本地主机?
- r - 更改数字向量的列名?
- python - 错误:y 无法将字符串转换为浮点 python 随机森林
- reactjs - 如何在 react-bootstrap-table2 (React.js) 中添加特定于行的操作按钮
- android - 如何在 Android 库中获取 Context