当测试功能被触发时,我正在尝试更新firestore中的文档,如下所示
import * as functions from 'firebase-functions';
import * as admin from 'firebase-admin'
admin.initializeApp(functions.config().firebase);
exports.test = functions.firestore.document('test/{id}').onCreate(event => {
admin.firestore().collection("testcollection").document(data.truckId)
})
但是这行代码不起作用
admin.firestore().collection("testcollection").document()
它给了我一个错误,文件功能不存在
编辑
我正在使用以下代码
admin.firestore().doc(`anothercollection/${data.id}`)
.get().then((result)=>{
});
它工作正常,但当使用以下命令部署功能时
firebase deploy --only functions
我得到以下错误
Failed to configure trigger providers/cloud.firestore/eventTypes/document.create@firestore.googleapis.com (test)
✔ functions[test(us-central1)]: Successful update operation.
Functions deploy had errors. To continue deploying other features (such as database), run:
firebase deploy --except functions
Error: Functions did not deploy properly.
collection()
返回firebase.firestore.CollectionReference
,而在类CollectionReference
中没有名为document()
的方法,这就是为什么会出现错误。
选项1:
-
导入
-
创建对集合"测试"下文档的引用
-
设置值
import { doc, setDoc, updateDoc } from 'firebase/firestore'; const docRef = doc(this.firestore, 'test', id); setDoc(docRef, { name: name }, { merge: true });
选项2:
updateDoc(docRef, { company: company });
它们之间的不同之处在于,如果文档不存在,第一个选项将创建它