Firestore書き込みイベントによってトリガされるFirebaseクラウド機能を書きたいとします。関数の内部では、書き込みが実際に作成、更新、または削除機能であるかどうかを知る必要があります。ロジックは少し異なりますが、私は3つの異なるイベントハンドラをコピーアンドペーストしたくありません。イベントの種類をどのように知ることができますか?Firestore +クラウド機能:作成、更新、または削除イベントかどうかを確認する方法
クラウドのドキュメントは、非常に混乱しています。
FireStoreの場合、イベントは実際にはDeltaDocumentSnapshotです。
https://firebase.google.com/docs/reference/functions/functions.Event
DeltaDocumentSnapshotのドキュメントは、クラスがイベント自体(作成、更新、または削除の例えば表示)に関する情報がありませんが、唯一のターゲットドキュメントことを示唆している:
https://firebase.google.com/docs/reference/functions/functions.firestore.DeltaDocumentSnapshot
サンプルが見つかりました。作成者は、現在のドキュメントまたは前のドキュメントが存在するかどうかをチェックして、作成/更新/削除を推測していたようです。私はそれを試みましたが、私はエラーが発生します。ここで
https://github.com/firebase/functions-samples/blob/master/child-count/functions/index.js#L30
私が試したものです:
exports.updateLikeCount = functions.firestore
.document('likes/{likeId}').onWrite((event) => {
if (event.data.data().exists()) {
// Looks like the document still exists. It must be an update or create.
}
if (event.data.previous.data().exists()) {
// Looks like the document existed before. It must be an update or delete.
}
コードは、このエラーで失敗した:
TypeError: event.data.exists is not a function
'child-count'からリンクするコードは' event.data.exists() 'を使用します。そこに余分な 'data()'を追加しましたが、これはうまくいきません。 –