Ronnie Smith
Ronnie Smith

Reputation: 18555

Firebase: Joining Firestore Document Results w/ JavaScript SDK

I'm wanting to get/read two documents with a Promise.all then insert some fields into one response which I got from the other, .then set to a final document.

I'm trying to do below and it doesn't error/fail but the data don't get transferred. I'm assuming I must "unpack" the responses, i.e., create a new object and append all the properties then hand that object off for the .set? ...Issue is that these responses can be full of stuff so I was hoping to not have to handle all that.

var promises = [getUserInfoFromFirestore(),getOrder(order,"orders")];

Promise.all(promises).then(function (res) {
    //move some user fields to order fields
    res[1].data().soldToEmail = res[0].email;
    finalRef.set(res[1].data()).then(function() {
        deleteOrder(order).then(function() {
            toast("Order Submitted");
        });
    });            
    res[1].data().soldToFirstName = res[0].firstName;
}).catch(function (error) {
    console.log("Error fetching order:", error);
}); 

Upvotes: 0

Views: 21

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317352

DocumentSnapshot objects are immutable. You will have to remember the results of the first call to data(), as it creates a new object each time. Modify that object instead, and use it in the call to set().

Alternatively, it's easier and more straightforward to use update() if you just want to modify the contents of a single field in a document. You don't even have to read the document you want to update.

Upvotes: 1

Related Questions