简体   繁体   中英

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, ie, 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);
}); 

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.

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