【发布时间】:2020-10-29 11:40:27
【问题描述】:
所以我想在文档上使用 onUpdate 触发器来获取前后的差异。
所以在示例中,文档之间的区别是“zfb5gXV54f5jbfn438Hd”。有了它,我将在我的应用程序的其他地方使用它。
这是我目前所拥有的:
export const someFunc = functions.firestore.document('invites/{invitesUID}').onUpdate(async (change, context) => {
const previousValue = change.before.data();
const newValue = change.after.data();
})
因此,通过 onUpdate,我将能够获得所需的信息。我应该采取什么方法呢?我有一些想法,但想知道是否有更好的方法来做到这一点。一如既往,感谢您的帮助!
更新:
所以我想出了这个:
export const deleteMember = functions.firestore.document('invites/{invitesUID}').onUpdate(async (change, context) => {
const previousValue = change.before.data()['invites']
const newValue = change.after.data()['invites']
if (previousValue.length > newValue.length) {
const prevInviteListArray = Object.entries(previousValue)
const newInviteListArray = Object.entries(newValue)
let difference = prevInviteListArray.filter((x: any) => !newInviteListArray.includes(x));
console.log('old list: ' + prevInviteListArray)
console.log('new list: ' + newInviteListArray)
console.log('the difference: ' + difference)
}
})
不知道为什么过滤器不起作用...有什么想法吗? (还有我怎样才能看到对象中的内容?)
【问题讨论】:
标签: typescript firebase google-cloud-firestore google-cloud-functions