Shubham Bisht
Shubham Bisht

Reputation: 687

How to add a collection inside document on Firestore? React Native

enter image description here

async componentDidMount() {

    firebase.firestore().collection('profiles').get().then(function(querySnapshot) {

        querySnapshot.forEach(function(doc) {

            var data = doc.data().pushToken; // I'm able to fetch pushToken here.

            const profileRef = firebase.firestore.doc(`profiles/0bT9eZ42CNN1uQYAE728WJht2aO2`)
                .collection('private').doc('pushToken');

            profileRef.set(data, {
                merge: true
            }).catch(e => console.error(e));

        });

    })
    .catch(function(error) {

        alert("error"); // My .then function throws an error and hence this alert works

        console.log("Error getting documents: ", error);
    });

}

I want to be able to fetch pushToken from each document's field and then insert into a collection within the same document by the name private. But it gives me an error saying

Error getting documents: TypeError: _reactNativeFirebase2.default.firestore.doc is not a function

How can I move pushToken to a collection? And I want to make the collection inside each document automatically and not manually.

Upvotes: 1

Views: 1743

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598623

Since you already have a DocumentSnapshot for the document that you want to create a subcollection under, you can build the CollectionReference and DocumentReference to the new document from that.

Something like this should work:

const profileRef = doc.ref.collection('private').doc('pushToken');

Upvotes: 3

Related Questions