リアルタイムデータベースに書き込む私のウェブサイトに連絡フォームを作成しています。クラウド機能を使用して、その連絡先の詳細が記載された電子メールを送信して、データベースエントリに返信したいと考えています。Firebase Cloud機能onWriteトリガを複数回起動する
私はペイロードを一度だけ送信し、データはデータベースに完全にうまく書き込まれていることを確認しました。私が抱えている問題は、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'文などを使用する必要があります。それは潜在的な原因として電子メールを検討するのを止めることができるので、問題を大幅に簡素化します。このようなことの詳細については、[MCVEの作成](http://stackoverflow.com/help/mcve)を参照してください。 –