admin管理员组文章数量:1130349
I want to write a Firebase cloud function that is triggered by a Firestore write event. Inside of the function, I have to know if the write is actually a create, update, or delete function. The logic is slightly different, but I don't want to copy and paste three different event handlers. How can I tell which event type it is?
The cloud documentation reads very confusing to me.
The documentation for Event says that in the case of FireStore, the event is actually a DeltaDocumentSnapshot.
.Event
The documentation for DeltaDocumentSnapshot suggests that the class has no information about the event itself (e.g. indication of create, update, or delete), but only the target document:
.firestore.DeltaDocumentSnapshot
I found samples, where the author seemed to infer create/update/delete by checking if the current or previous document exists. I've tried that but I get an error.
.js#L30
Here is what I've tried:
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.
}
That code fails with this error:
TypeError: event.data.exists is not a function
I want to write a Firebase cloud function that is triggered by a Firestore write event. Inside of the function, I have to know if the write is actually a create, update, or delete function. The logic is slightly different, but I don't want to copy and paste three different event handlers. How can I tell which event type it is?
The cloud documentation reads very confusing to me.
The documentation for Event says that in the case of FireStore, the event is actually a DeltaDocumentSnapshot.
https://firebase.google./docs/reference/functions/functions.Event
The documentation for DeltaDocumentSnapshot suggests that the class has no information about the event itself (e.g. indication of create, update, or delete), but only the target document:
https://firebase.google./docs/reference/functions/functions.firestore.DeltaDocumentSnapshot
I found samples, where the author seemed to infer create/update/delete by checking if the current or previous document exists. I've tried that but I get an error.
https://github./firebase/functions-samples/blob/master/child-count/functions/index.js#L30
Here is what I've tried:
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.
}
That code fails with this error:
TypeError: event.data.exists is not a function
Share
Improve this question
edited Apr 23, 2024 at 20:24
benomatis
5,6437 gold badges39 silver badges60 bronze badges
asked Nov 26, 2017 at 17:36
Thomas FischerThomas Fischer
1,5142 gold badges15 silver badges26 bronze badges
1
-
The code you link from
child-countusesevent.data.exists(). You've added an extradata()in there, which doesn't work. – Frank van Puffelen Commented Nov 26, 2017 at 21:09
2 Answers
Reset to default 15With the new release of firebase-functions version 1.0, a function to check this could look like this (in typescript):
import { Change } from 'firebase-functions';
export function checkEventType(change: functions.Change<FirebaseFirestore.DocumentSnapshot>): string {
const before: boolean = change.before.exists;
const after: boolean = change.after.exists;
if (before === false && after === true) {
return 'create';
} else if (before === true && after === true) {
return 'update';
} else if (before === true && after === false) {
return 'delete';
} else {
throw new Error(`Unknown firestore event! before: '${before}', after: '${after}'`);
}
}
I have tested this function, and works great in bination with the .onWrite() event.
I've tried a few different things. The version below seems to work, yet I don't know if it is correct.
var exists = event.data.exists;`
var hasExisted = (event.data.previous != null) && event.data.previous.exists;
var isCreate = (exists && !hasExisted);
var isDelete = (!exists && hasExisted);
I want to write a Firebase cloud function that is triggered by a Firestore write event. Inside of the function, I have to know if the write is actually a create, update, or delete function. The logic is slightly different, but I don't want to copy and paste three different event handlers. How can I tell which event type it is?
The cloud documentation reads very confusing to me.
The documentation for Event says that in the case of FireStore, the event is actually a DeltaDocumentSnapshot.
.Event
The documentation for DeltaDocumentSnapshot suggests that the class has no information about the event itself (e.g. indication of create, update, or delete), but only the target document:
.firestore.DeltaDocumentSnapshot
I found samples, where the author seemed to infer create/update/delete by checking if the current or previous document exists. I've tried that but I get an error.
.js#L30
Here is what I've tried:
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.
}
That code fails with this error:
TypeError: event.data.exists is not a function
I want to write a Firebase cloud function that is triggered by a Firestore write event. Inside of the function, I have to know if the write is actually a create, update, or delete function. The logic is slightly different, but I don't want to copy and paste three different event handlers. How can I tell which event type it is?
The cloud documentation reads very confusing to me.
The documentation for Event says that in the case of FireStore, the event is actually a DeltaDocumentSnapshot.
https://firebase.google./docs/reference/functions/functions.Event
The documentation for DeltaDocumentSnapshot suggests that the class has no information about the event itself (e.g. indication of create, update, or delete), but only the target document:
https://firebase.google./docs/reference/functions/functions.firestore.DeltaDocumentSnapshot
I found samples, where the author seemed to infer create/update/delete by checking if the current or previous document exists. I've tried that but I get an error.
https://github./firebase/functions-samples/blob/master/child-count/functions/index.js#L30
Here is what I've tried:
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.
}
That code fails with this error:
TypeError: event.data.exists is not a function
Share
Improve this question
edited Apr 23, 2024 at 20:24
benomatis
5,6437 gold badges39 silver badges60 bronze badges
asked Nov 26, 2017 at 17:36
Thomas FischerThomas Fischer
1,5142 gold badges15 silver badges26 bronze badges
1
-
The code you link from
child-countusesevent.data.exists(). You've added an extradata()in there, which doesn't work. – Frank van Puffelen Commented Nov 26, 2017 at 21:09
2 Answers
Reset to default 15With the new release of firebase-functions version 1.0, a function to check this could look like this (in typescript):
import { Change } from 'firebase-functions';
export function checkEventType(change: functions.Change<FirebaseFirestore.DocumentSnapshot>): string {
const before: boolean = change.before.exists;
const after: boolean = change.after.exists;
if (before === false && after === true) {
return 'create';
} else if (before === true && after === true) {
return 'update';
} else if (before === true && after === false) {
return 'delete';
} else {
throw new Error(`Unknown firestore event! before: '${before}', after: '${after}'`);
}
}
I have tested this function, and works great in bination with the .onWrite() event.
I've tried a few different things. The version below seems to work, yet I don't know if it is correct.
var exists = event.data.exists;`
var hasExisted = (event.data.previous != null) && event.data.previous.exists;
var isCreate = (exists && !hasExisted);
var isDelete = (!exists && hasExisted);
本文标签:
版权声明:本文标题:javascript - Firestore + cloud function: How to tell if it's a create, update, or delete event - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://it.en369.cn/questions/1740092617a1711723.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


发表评论