te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Firestore + cloud function: How to tell if it's a create, update, or delete event - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Firestore + cloud function: How to tell if it's a create, update, or delete event - Stack Overflow

programmeradmin3浏览0评论

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-count uses event.data.exists(). You've added an extra data() in there, which doesn't work. – Frank van Puffelen Commented Nov 26, 2017 at 21:09
Add a ment  | 

2 Answers 2

Reset to default 15

With 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);

与本文相关的文章

发布评论

评论列表(0)

  1. 暂无评论