简体   繁体   中英

I'm new in firebase and I'm trying to get id document for parent collection but I can't figure it out

I have collection (Categories) and subcollection (product) and I try to delete the document in subcollection with id so I need id for Parent document how to get it?

cross.addEventListener('click',(e) => {
    e.stopPropagation();
    let id =e.target.parentElement.getAttribute('data-id');

    db.collection("Categories").doc("parent.id").collection("Product").doc(id).delete();
    })

    });

}

You cannot know the parent information unless you have the snapshot/reference of the document/sub-collection under the parent whose id you need. The snapshots have a property 'parent' just like 'id'.

SubCollectionReference = DocumentSnapshot.ref.parent;

You can just use parent.parent to find out the document hosting the sub-collection.

ParentDocumentReference = DocumentSnapshot.ref.parent.parent;

If you just know the document id but do not have a snapshot or reference to the document, I suggest you to create a reference field to this document or just the parent document directly where ever you are pulling this id information from.

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