I’m new to firebase in general and am trying to write parts of an array into a document and its remaining data in a document in a subcollection. Here is what I’ve done:
let db = admin.firestore();
db.collection("User")
.doc(fields.user)
.collection("Address")
.doc(fields.address)
.set({
User: fields.user,
Address: fields.address,
})
.then(
db.collection("User")
.doc(fields.user)
.collection("Address")
.doc(fields.address)
.collection("Orders")
.doc(fields.ID)
.set({
ID: fields.ID,
});
My biggest question is if there is a better way to do this?
Here its a small sample but for cases it can get very long.
Thanks in advance for any help.
2
Answers
I often prefer constructing longer paths using string interpolation, which would lead to:
Aside from that, you probably want to do this in a batched write so that either both of the writes succeed, or neither of them does.
if you have all the data in the first time.
you can create your object with subcollections and then set it in one request