gpt4 book ai didi

firebase - 如何调用firestore数据库的onwrite事件监听函数?

转载 作者:行者123 更新时间:2023-12-02 20:09:29 29 4
gpt4 key购买 nike

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

如何调用firestore数据库的onwrite事件监听函数?

最佳答案

当 firestore 和 firebase 的数据库发生任何更改时调用“onwrite”事件函数。

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 中添加、更新和删除数据。

关于firebase - 如何调用firestore数据库的onwrite事件监听函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53933463/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com