【发布时间】:2018-06-12 09:12:45
【问题描述】:
我的应用程序有一个简单的云功能来跟踪创建/更新的时间戳。它看起来像这样:
export const objectChanges = functions
.firestore
.document('objects/{id}')
.onWrite(event => {
const patch: {created_at?: string, updated_at: string} = {
updated_at: <string> event.timestamp
};
if (!event.data.previous) {
patch.created_at = event.timestamp;
}
return event.data.ref.set(patch, {merge: true});
});
只要我上传这个函数并在列表中创建/修改一个对象,它就会开始不断地勾选 updated_at。我猜它正在检测它本身对 updated_at 字段所做的更改。考虑到文档显示了这种更新的示例,这种行为让我感到困惑。
https://firebase.google.com/docs/functions/firestore-events#writing_data
是否有我遗漏的细微差别,或者这是 Firestore 错误?
【问题讨论】:
-
看起来你可能遗漏了这部分:"注意:任何时候你写入触发函数的同一个文档,你都有创建无限循环的风险。请谨慎行事,并确保在不需要更改时安全退出函数。”似乎没有任何示例证明了这一点。
标签: javascript node.js firebase google-cloud-functions google-cloud-firestore