正确链接功能在火力地堡功能

问题描述 投票:0回答:2

我建立在火力地堡云功能的功能,它可以利用Node.js的模块。

我还是新来的使用.then()的,我奋力的方式找出该链我的3个功能webhookSend()emailSendgrid()removeSubmissionProcessor()'count'递增后(if语句来检查temp_shouldSendWebhook)是正确的发生。返回承诺的整体思路仍然让我困惑了一点,特别是当它涉及外部库。

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 = '[email protected]'
  const name = 'Test name'

  const msg = {
      to: user,
      from: '[email protected]',
      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();
}

我希望能够构建3个功能被称为彼此运动,使得他们都将执行后。

javascript node.js firebase firebase-realtime-database google-cloud-functions
2个回答
3
投票

为了链这些功能,他们每个人都需要返回一个承诺。当他们这样做,你可以依次调用它们像这样:

return webhookSend()
  .then(() => {
    return emailSendgrid();
  })
  .then(() => {
    return removeSubmissionProcessor();
  });

或者,在这样的并行:

return Promise.all([webhookSend, emailSendgrid, removeSubmissionProcessor]);

现在,让你的函数返回的承诺:

emailSendgrid:它看起来像这将返回一个承诺(假设sgMail.send(msg)返回一个承诺),所以你不应该需要改变这一点。

removeSubmissionProcessor:此调用返回一个承诺的功能,但不返回的承诺。换句话说,它触发关闭一个异步调用(admin.database....remove()),但不等待响应。如果您在电话前加return,这应该工作。

webhookSend调用一个函数,需要一个回调,所以你要么需要使用fetch(这是承诺为基础的),而不是request,或者您需要将其转换为以返回承诺链吧:

function webhookSend() {
  return new Promise((resolve, reject) => {
    request.post(
      {
        url: 'URLHERE',
        form: {test: "value"}
      },
      function (err, httpResponse, body) {
        console.log('REQUEST RESPONSE', err, body);
        if (err) {
          reject(err);
        } else {
          resolve(body);
        }
      }
    );
  });
}

0
投票

每一个函数调用之前使用异步函数,然后就可以使用。那么()或等待

参考阅读this

© www.soinside.com 2019 - 2024. All rights reserved.