Firebase Cloud函数无限循环调用

时间:2018-10-18 22:18:05

标签: angular typescript firebase google-cloud-functions

我具有用于发送电子邮件的云功能,该功能托管在附属的Firebase应用程序中。超时设置为60秒,并通过https请求调用。但是,从UI发出请求时,即使发送电子邮件,也会每60秒调用一次,然后超时。

Component.ts -表单提交操作

      onSubmit() {
          this.cloudService.sendEmail(
              this.model.name,
              this.model.email,
              this.model.message
          );
        }

CloudFunctionService.ts

    export class CloudFunctionService {
      constructor(private http: HttpClient) {}

      sendEmail(name, email, message) {
        const url = 'https://url/path/to/function';

        const params = new HttpParams()
          .set('name', name)
          .set('email', email)
          .set('message', message);

        return this.http.get(url, {params}).toPromise()
          .then(res => {
            // this line never gets hit
            console.log(res);
          })
          .catch(err => {
            // this line never gets hit
            console.log(err);
          });
      }
    }

index.ts -功能的打字稿代码

    import * as functions from 'firebase-functions';
    import * as sgMail from '@sendgrid/mail';
    import * as cors from 'cors';
    const corsHandler = cors({origin: true});

    export const sendContactMail = functions.https.onRequest((request, response) => {
      corsHandler(request, response, () => { console.log('cors handled')});

      const SENDGRID_API_KEY = functions.config().sendgrid.key;
      sgMail.setApiKey(SENDGRID_API_KEY);
      sgMail.setSubstitutionWrappers('{{','}}');

      const msg = {
        to: 'some@email.com',
        from: 'some.other@email.com',
        templateId: 'mytemplateid',
        substitutions: {
          name: request.query.name,
          email: request.query.email,
          message: request.query.message
        }
      };

        sgMail.send(msg).then((res) => {
            console.log(res);
           }).catch(err => {
            console.log(err);
          });

        response.send();
    });

Firebase功能日志

Phantom re-triggered every 60s only to time out again

Even though I am receiving the emails from the function

1 个答案:

答案 0 :(得分:0)

HTTP类型的函数必须向客户端发送响应才能完成该功能。您的函数未在默认超时内向客户端发送任何内容,因此Cloud Functions必须假定它失败。由于可能会失败,因此它可能还会重试该功能。

来自documentation

  

使用res.redirect(),res.send()或res.end()终止HTTP函数。