Как вызвать функцию прослушивания событий onwrite для базы данных firestore? - PullRequest
0 голосов
/ 26 декабря 2018
functions.firestore
    .document('users/00QAGyS0NqFdDSS78E6r')
    .onWrite(event => {

        const commentId = event.params.commentId;
        const postId = event.params.postId;

        // ref to the parent document
        const docRef = admin.firestore().collection('posts').doc();

        // get all comments and aggregate
        return docRef.collection('comments').orderBy('createdAt', 'desc')
            .get()
            .then(querySnapshot => {

                // get the total comment count
                const commentCount = querySnapshot.size

                const recentComments = []

                // add data from the 5 most recent comments to the array
                querySnapshot.forEach(doc => {
                    recentComments.push( doc.data() )
                });

                recentComments.splice(5)

                // record last comment timestamp
                const lastActivity = recentComments[0].createdAt

                // data to update on the document
                const data = { commentCount, recentComments, lastActivity }

                // run update
                return docRef.update(data)
            })
            .catch(err => console.log(err) )
});

Как вызвать функцию прослушивания события onwrite для базы данных firestore?

1 Ответ

0 голосов
/ 14 февраля 2019

Функция события «onwrite» вызывается, когда происходит какое-либо изменение в базе данных firestore и firebase.

exports.eventTriggerUserData = functions.firestore
.document('users/{userId}')
.onWrite((change: any, context: any) => {
    /* Get user Id on which action perform
    const userId = context.params.userId; */

    /* Get data new data after action perform */
    const documentAfter = change.after.exists ? change.after.data() : null;

    /* Get data old data before action perform */
    const documentBefore = change.before.exists ? change.before.data() : null;
});

Эту функцию необходимо развернуть на firebase и с добавлением, обновлением и удалением данных в firestore.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...