xblaz3kx
xblaz3kx

Reputation: 371

Firebase Functions onCreate won't execute a part of my code

So I've been writing some code to sync my Firestore with another program's database so I've added references.

I want my onCreate method to get a reference number from a seller entry, make a new document with that reference number as well as update orderReference field in the existing document (I want the database to handle it instead of making queries on the client/app), delete the previous order since it's ID can't be updated from functions and update the orderNumber field.

I don't get a new document created nor updated values. I'm really new to Node.js and promises. Any tips and suggestions are welcome.

    exports.createOrderReference = functions.firestore.document('/orders/{order}').onCreate((snapshot, context) => {
    var id = snapshot.id; //id of order document
    var sellerID = snapshot.data().sellerID;

    var orderNumber = 0;
    var content;
    var sellerSnapID;
    return admin.firestore().collection('sellers').where("ID", "==", sellerID).get().then((qSnapshot) => {
        var docs = qSnapshot.docs;
        orderNumber = docs[0].data().orderReferenceNumber;
        sellerSnapID = docs[0].id;
        content = snapshot.data();
        Object.assign(content, {orderReference: orderNumber});
        console.log(content); // <----- last execution
        return content;
    }).then(() => {
        return admin.firestore().collection('orders').doc(orderNumber).set(content).then(() => {
            var promises = [];
            promises.push(admin.firestore().collection('orders').doc(id).delete());
            orderNumber++;
            promises.push(admin.firestore().collection('sellers').doc(sellerSnapID).update({orderReferenceNumber: orderNumber}));
            return Promise.all(promises);
        });
    }).catch((err) => {
        return err;
    });

Updated code

exports.createOrderReference = functions.firestore.document('/orders/{order}').onCreate((snapshot, context) => {
    var id = snapshot.id; //id of order document
    var sellerID = snapshot.data().sellerID;

    var orderNumber = 0;
    var content;
    var sellerSnapID;
    return admin.firestore().collection('sellers').where("ID", "==", sellerID).get().then((qSnapshot) => {
        var docs = qSnapshot.docs;
        orderNumber = docs[0].data().orderReferenceNumber;
        var orderID = docs[0].data().orderReferenceNumber;
        sellerSnapID = docs[0].id;
        content = snapshot.data();
        content.orderReference = orderNumber;

        return admin.firestore().collection('orders').doc(orderID).set(content);
    }).then(() => {
        orderNumber++;
        var promises = [];
        promises.push(admin.firestore().collection('orders').doc(id).delete());
        promises.push(admin.firestore().collection('sellers').doc(sellerSnapID).update({orderReferenceNumber: orderNumber}));
        return Promise.all(promises);
    }).catch((err) => {
        return err;
    });
});

Upvotes: 0

Views: 359

Answers (1)

Renaud Tarnec
Renaud Tarnec

Reputation: 83058

If you want to chain the promises you need to return a promise within then().

See https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/then#Chaining, which explains that:

If the function passed as handler to then returns a Promise, an equivalent Promise will be exposed to the subsequent then in the method chain.

Currently, in your first then you return content, which is not a promise. You should modify your code along the following lines:

exports.createOrderReference = functions.firestore.document('/orders/{order}').onCreate((snapshot, context) => {
var id = snapshot.id; //id of order document
var sellerID = snapshot.data().sellerID;

//....

return admin.firestore().collection('sellers').where("ID", "==", sellerID).get()
.then((qSnapshot) => {
    //.....
    //Set the value of content

    //Return a promise (from the set() method)     
    return admin.firestore().collection('orders').doc(orderNumber).set(content);
})
.then(() => {
    //.....

    //Return a promise, as you do with   
    return Promise.all(promises);

})
.then(results => {
    //.....
    //Return a promise
}).catch((err) => {
    console.log(err)
    return null;
});

You may also watch the official Firebase video series here: https://firebase.google.com/docs/functions/video-series/. In particular watch the three videos titled "Learn JavaScript Promises" (Parts 2 & 3 especially focus on background triggered Cloud Functions, but it really worth watching Part 1 before).

Upvotes: 1

Related Questions