英文:
Copy a firestore document to another collection from firebase trigger function
问题
I want to sync my doc to another collection based on the firebase trigger function
.
我想根据 firebase 触发函数
将我的文档同步到另一个集合。
exports.foo = functions
.region("foo")
.firestore.document("foo")
.onWrite((events, context) => {
return new Promise((resolve, reject) => {
if (events.after.exists) {
// on create or update
admin
.firestore().doc("newFoo")
.set(events.after.data)
.then(() => { resolve(true); })
.catch((ex) => { reject(ex); });
} else{
// on delete
admin
.firestore()
.doc("newFoo")
.delete()
.then(() => { resolve(true); })
.catch((ex) => { reject(ex); });
}
});
});
但是当我尝试在 Firestore 中手动编辑我的文档时,我收到以下错误:
> Error: Value for argument "data" is not a valid Firestore document.
> Input is not a plain JavaScript object.
为什么会出现这个错误?根据我的检查,events.after.data
也返回了一个 FirebaseFirestore.DocumentData
,这是 doc.set()
的正确数据类型。
英文:
I want to sync my doc to another collection based on the firebase trigger function
exports.foo = functions
.region("foo")
.firestore.document("foo")
.onWrite((events, context) => {
return new Promise((resolve, reject) => {
if (events.after.exists) {
// on create or update
admin
.firestore().doc("newFoo")
.set(events.after.data)
.then(() => { resolve(true); })
.catch((ex) => { reject(ex); });
} else{
// on delete
admin
.firestore()
.doc("newFoo")
.delete()
.then(() => { resolve(true); })
.catch((ex) => { reject(ex); });
}
});
});
but I got the following error when I try to manually edit my document in firestore
> Error: Value for argument "data" is not a valid Firestore document.
> Input is not a plain JavaScript object.
why do I get the error? as I check, the events.after.data
is also returning a FirebaseFirestore.DocumentData
which is the correct data type for doc.set()
答案1
得分: 1
.set(events.after.data)
应该改为 .set(events.after.data())
,因为 data
是一个方法而不是属性。
英文:
This line .set(events.after.data)
should be .set(events.after.data())
as data is a method not a property.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论