【发布时间】:2018-06-06 21:58:06
【问题描述】:
我尝试在 Firebase 上放置一个侦听器,该侦听器将复制 Firestore 中匹配元素中的值。
exports.synchronizeDelegates = functions.database.ref(`delegates/{userId}/activities`).onUpdate((event) => {
const userKey = event.data.ref.parent.key
console.log("User Key:" + userKey)
return admin.database().ref(`delegates/${userKey}/email`).once('value', snapshot => {
let email = snapshot.val()
console.log("Exported Email:" + email)
const userRef = admin.firestore().collection('users')
const firestoreRef = userRef.where('email', "==", email)
firestoreRef.onSnapshot().update({ activities: event.data.toJSON() })
}).then(email => {
console.log("Firebase Data successfully updated")
}).catch(err => console.log(err))
}
)
此函数能够检索和定位在firestore中定位正确文档所需的元素,但.update()函数仍然错误firestoreRef.update is not a function
我尝试了几种查询方法,但仍然出现此错误。
在这种情况下如何正确查询并更新文档?
【问题讨论】:
-
你有没有试过用这个替换那行:
firestoreRef.update({ activities: event.data.toJSON() })? -
是的,我尝试在查询之后链接 update() 方法,它返回相同的错误。
-
顺便说一句:我喜欢您使用 Cloud Functions 将 Cloud Firestore 同步到实时数据库。
-
我不确定@FrankvanPuffelen 有多大的讽刺意味,但在我的情况下,我的 RTDB 中的
activities字段是由第三方供应商更新的,该供应商通过API。当我为我的客户视图继续使用 Firestore 时,这是我发现使其工作的最快方法,因为此时我无法控制第三方开发。下一步将很明显,更新第三方提供程序以在 Firestore 中写入 -
一点也不讽刺。对不起,如果它是这样的。我一直想为我的一些项目编写这样的 RTDB 到 Firestore 同步功能,但是……太多其他的东西不断出现。很高兴看到至少有人做到了。 :-)
标签: javascript firebase firebase-realtime-database google-cloud-functions google-cloud-firestore