【发布时间】:2019-02-06 02:49:39
【问题描述】:
我正在 Firebase Cloud Functions 中构建一个可以利用 Node.js 模块的函数。
我对@987654322@ 的使用仍然很陌生,我正在努力寻找一种方法来链接我的 3 个函数 webhookSend()、emailSendgrid() 和 removeSubmissionProcessor(),这发生在 'count' 之后递增(检查 temp_shouldSendWebhook 的 if 语句)。返回承诺的整个想法仍然让我有些困惑,尤其是当它涉及外部库时。
const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp();
const request = require('request');
const firebaseConfig = JSON.parse(process.env.FIREBASE_CONFIG);
const SENDGRID_API_KEY = firebaseConfig.sendgrid.key;
const sgMail = require('@sendgrid/mail');
sgMail.setApiKey(SENDGRID_API_KEY);
exports.submissionProcess = functions.database.ref('/submissions/processor/{submissionId}').onWrite((change, context) => {
var temp_metaSubmissionCount = 0; // omitted part of function correctly sets the count
var temp_shouldSendWebhook = true; // omitted part of function correctly sets the boolean
return admin.database().ref('/submissions/saved/'+'testuser'+'/'+'meta').child('count')
.set(temp_metaSubmissionCount + 1)
.then(() => {
// here is where im stuck
if (temp_shouldSendWebhook) {
webhookSend();
emailSendgrid();
removeSubmissionProcessor();
} else {
emailSendgrid();
removeSubmissionProcessor();
}
})
.catch(() => {
console.error("Error updating count")
});
});
function emailSendgrid() {
const user = 'test@example.com'
const name = 'Test name'
const msg = {
to: user,
from: 'hello@angularfirebase.com',
subject: 'New Follower',
// text: `Hey ${toName}. You have a new follower!!! `,
// html: `<strong>Hey ${toName}. You have a new follower!!!</strong>`,
// custom templates
templateId: 'your-template-id-1234',
substitutionWrappers: ['{{', '}}'],
substitutions: {
name: name
// and other custom properties here
}
};
return sgMail.send(msg)
}
function webhookSend() {
request.post(
{
url: 'URLHERE',
form: {test: "value"}
},
function (err, httpResponse, body) {
console.log('REQUEST RESPONSE', err, body);
}
);
}
function removeSubmissionProcessor() {
admin.database().ref('/submissions/processor').child('submissionkey').remove();
}
我希望能够构建三个函数一个接一个地调用,这样它们都会执行。
【问题讨论】:
-
如果您对在 Cloud Functions 中使用 Promise 感到困惑,您应该观看文档中的视频系列。 firebase.google.com/docs/functions/video-series
标签: javascript node.js firebase firebase-realtime-database google-cloud-functions