我想做的是在Firebase数据库中创建一个文档引用,该文档引用会返回一个ID和一个文档引用,因此我可以使用该ID在存储中为该对象的图像创建路径。
因此,根据firebase docs,我应该能够做到这一点:
var newPlantRef = db.collection(`users/${this.userId}/plants`).doc();
但这将返回此错误:
AddPlantComponent.html:30 ERROR FirebaseError: Function CollectionReference.doc() requires its first argument to be of type non-empty string,but it was: undefined
所以我在Github上找到了一种解决方法,说我应该这样做:
const id = this.afs.createId();
const ref = this.afs.collection(`users/${this.userId}/plants`).doc(id);
但是当我这样做时:
this.newPlantId = this.db.createId();
this.newPlantRef = this.db.collection(`users/${this.userId}/plants`).doc(this.newPlantId).ref;
this.newPlantRef.update({
name: 'Test1',primaryImgURL: 'test1',addedDate: new Date()
});
}
我收到此错误:
ERROR Error: Uncaught (in promise): FirebaseError: [code=not-found]: No document to update: projects/plant-app-d9298/databases/(default)/documents/users/UoV0MXMfZGhMgmMJHpiMU41aLZG2/plants/LWmM79JotgDn20hPOcA1
FirebaseError: No document to update: projects/plant-app-d9298/databases/(default)/documents/users/UoV0MXMfZGhMgmMJHpiMU41aLZG2/plants/LWmM79JotgDn20hPOcA1
我做错什么了吗?还有其他解决方法,可以在添加之前获取文档ID,以便我的物品和存储可以引用该ID?