MrTouya
MrTouya

Reputation: 656

How can I add a field to a document in Firestore and then add a collection?

I am trying to do the following:

enter image description here

I am using the following call:

 db.collection("TagEvents").doc(rfid).add({ currentStatus: riderOnBus }, { merge: true }).collection(eventDate).add(jsonDeviceMessage)
                    .then(function (data) {
                        console.log('Firebase data: ', data);
                        context.succeed();                 
                    })
                    .catch(function (error) {
                        console.log('Firebase error: ', error);
                        context.fail();
                    });

It won't let me add a collection after I add a field to the doc!!

What am I doing wrong???

Thanks :-)

Upvotes: 1

Views: 52

Answers (1)

Alex Mamo
Alex Mamo

Reputation: 138824

It won't let me add a collection after I add a field to the doc!!

There is no way you can add a document and a subcollection in a single go. To solve this, you should do two separate operations. The first one would be to add the document to your TagEvents collection:

db.collection("TagEvents").doc(rfid).add({ currentStatus: riderOnBus }, { merge: true }).then(/* ... */);

And the second one would be to create a new reference and add jsonDeviceMessage to your eventDate subcollection.

db.collection("TagEvents").doc(rfid).collection(eventDate)
    .add(jsonDeviceMessage).then(/* ... */);

Upvotes: 1

Related Questions