从Angular 4组件调用Firebase Cloud函数
我正在使用Angular 4、Firebase数据库和云功能。问题:如何从组件中单击调用Firebase函数 我试着做的就是:点击按钮发送带有emailjs的测试电子邮件 我的代码: 函数/src/index.ts从Angular 4组件调用Firebase Cloud函数,angular,typescript,firebase,google-cloud-functions,Angular,Typescript,Firebase,Google Cloud Functions,我正在使用Angular 4、Firebase数据库和云功能。问题:如何从组件中单击调用Firebase函数 我试着做的就是:点击按钮发送带有emailjs的测试电子邮件 我的代码: 函数/src/index.ts import * as functions from 'firebase-functions'; const emailjs = require("emailjs/email"); export const sendMail = functions.https.onRequest(
import * as functions from 'firebase-functions';
const emailjs = require("emailjs/email");
export const sendMail = functions.https.onRequest((request, response) => {
var email = require("./path/to/emailjs/email");
var server = email.server.connect({
user: "username",
password:"password",
host: "smtp.your-email.com",
ssl: true
});
// send the message and get a callback with an error or details of the message that was sent
server.send({
text: "i hope this works",
from: "you <username@your-email.com>",
to: "someone <someone@your-email.com>, another <another@your-email.com>",
cc: "else <else@your-email.com>",
subject: "testing emailjs"
}, function(err, message) { console.log(err || message); });
})
因此:如何从我的组件调用Firebase函数?您使用的是HTTP云函数,因此您必须通过调用特定URL来触发它,如文档中所述: 部署HTTP函数后,您可以通过自己的函数调用它 唯一的URL。URL按顺序包括以下内容:
- The region in which your function is deployed
- Your Firebase project ID
- cloudfunctions.net
- The name of your function
因此,在您的情况下,调用sendMail()的URL是:
https://us-central1-.cloudfunctions.net/sendMail
您应该使用HttpClient服务触发此调用
最后,我建议您观看Firebase团队的以下视频,其中详细介绍了如何编写HTTP云功能,特别是如何向调用方发送响应: 您可能会看到,您需要相应地稍微调整代码
- The region in which your function is deployed
- Your Firebase project ID
- cloudfunctions.net
- The name of your function
https://us-central1-<your-project-id>.cloudfunctions.net/sendMail