【问题标题】:Firebase Cloud Function onWrite trigger firing multiple timesFirebase Cloud Function onWrite 触发器多次触发
【发布时间】:2018-05-16 09:31:31
【问题描述】:

我正在我的网站上创建一个写入实时数据库的联系表单。使用 Cloud Functions,我想通过向自己发送一封包含联系人详细信息的电子邮件来响应数据库条目。

我已经确认我只发送了一次有效负载,并且数据正在完美地写入数据库。我遇到的问题是 onWrite 响应数据库的每一行条目,例如我发送姓名、电子邮件、公司和消息,因此,我的函数调用了 4 次。我不会重写数据库,我知道它会执行另一个调用。每次调用时都会收到一行数据作为单个值,我没有在文档中获得所有值的对象作为“承诺”。我也尝试过 onCreate,它甚至没有被调用。

我是不是被误导了?这是预期的行为吗?任何帮助表示赞赏。

代码...

const functions = require('firebase-functions');
const admin = require('firebase-admin');
admin.initializeApp(functions.config().firebase);

exports.sendContactMessage = functions.database
  .ref('/messages/{pushKey}')
  .onWrite(event => {
    const snapshot = event.data;
    // Only send email for new messages.
    if (!snapshot.exists()) { return null; }

    const val = snapshot.val();

    console.log(val);
  });

我的班级发布到“消息”...

import { Component } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { AngularFireDatabase, AngularFireObject } from 'angularfire2/database';
import * as firebase from 'firebase/app';

export interface Item { name: string; email: string; message: string; html: string; date: string }

@Component({
  selector: 'app-contact',
  templateUrl: './contact.component.html',
  styleUrls: ['./contact.component.scss']
})
export class ContactComponent {
      public rForm: FormGroup;
      public post: any;
      public message: string = '';
      public name: string = '';
      public company: string = '';
      public email: string = '';
      itemRef: AngularFireObject<any[]>;
      item;

  constructor(private fb: FormBuilder, public db: AngularFireDatabase) {
    this.createForm();
    this.itemRef = db.object('messages');
    this.item = this.itemRef.valueChanges();
  }

  createForm() {
    this.rForm = this.fb.group({
      name: [null, Validators.required],
      email: [null, Validators.compose([Validators.required, Validators.email])],
      message: [null, Validators.compose([Validators.required, Validators.minLength(15)])],
      validate: ''
    });
  }

  sendMessage(post) {

    this.name = post.name;
    this.email = post.email;
    this.message = post.message;

    const item: Item = {
      name: post.name,
      email: post.email,
      message: post.message,
      html: `You were contacted from your website by ${post.name}. They said "${post.message}". You can contact them back on ${post.email}`,
      date: Date()
    };

    this.itemRef.update(item);
  }
}

【问题讨论】:

  • 将写入的代码贴到/messages
  • 虽然理论上有可能多次触发一个函数,但我还没有看到这种情况发生。正如 Bob 所问的,请更新您的问题以包含触发该功能的代码。另外:如果该函数确实被多次触发,如果您不使用nodemailer,也应该这样做,但例如在正文中只使用console.log 语句。这将大大简化问题,因为我们可以停止将电子邮件视为潜在原因。阅读creating an MCVE 了解更多关于此类的信息。

标签: angular firebase google-cloud-functions nodemailer firebase-cloud-functions


【解决方案1】:

我怀疑任何人都像我一样愚蠢,但是,我正在向我的数据库中的同一个位置写信。仅存在 1 个条目,因此我“更改”了 4 项数据,因此我将触发 4 个 onWrite 事件。

我通过在数据库中的唯一文件夹下创建一个新条目来解决此问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-08-25
    • 2020-10-26
    • 2018-10-11
    • 2018-06-25
    • 2021-09-14
    • 1970-01-01
    • 2018-08-09
    • 2019-12-16
    相关资源
    最近更新 更多