简体   繁体   中英

How to write data in a subcollection? Firebase React

I wish to be able to add projects in a subcollection called projects. Currently my tree is: db/membres/[membreID]/projects/(here I want project ID that firebase generates)/(information of project) Here is my firebase: 在此处输入图像描述

here's what i've tried: 在此处输入图像描述

This is the error I get: Invalid document reference. Document references must have an even number of segments, but membres/UVLPL9kxP1hGuiwCStS4oDLJvxo1/projets has 3.

The error message is quite clear: you are trying to write to a document at path /membres/$uid/projects , but that is the path of a collection.

If you want to add a document to that subcollection, use addDoc instead of setDoc .

addDoc works like that:

addDoc(CollectionReference //i.e collection(db, "path"), element)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM