'Firebase Functions sporadically updating doc

I´m receiving Webhooks from an online store about categories created. The following function creates the document inside the collection categories

exports.createProductWebhookCategory = functions
    .https.onRequest(async (request, response) => {
        var category = request.body.product_type;
        try {
            const categoryRef = admin.firestore().collection("categories").doc(`${category}`);
            await categoryRef.set({
                name: category,
            });
            response.status(200).send("Done");
        } catch (error) {
            console.log(error);
            response.status(400).send("Error Cat");
        }
    });

After the category is created I'm calling an API to create an item inside Webflow. With the returned promise I get the item Id I want to store inside the previous created document

I´m trying this with 5 categories (5 webhooks) and just 1 or 2 out of 5 are updated. The other documents are not updated and do not contain the webflowId field. The updated ones change with each test run. Anybody an idea what I'm doing wrong?

exports.onCreateCategoryCallback = functions
    .runWith({ failurePolicy: true })
    .firestore
    .document("/categories/{categoryId}")
    .onCreate(async (snapshot, context) => {
        const cat = snapshot.data();
        const docId = context.params.categoryId;
        const categoryRef = admin.firestore().collection("categories").doc(docId);
        try {
            const webflow_item = await webflow.createItem({
                collectionId: 'xxx',
                fields: {
                    'name': cat.name,
                    '_archived': false,
                    '_draft': false,
                },
            }, { live: true });
            console.log(`ItemId for Cat ${cat.name} is ${webflow_item._id}`);
            const doc = await categoryRef.get();
            console.log(doc.data());
            const res = await categoryRef.update({
                webflowId: webflow_item._id
            });
            console.log(`RES for ${cat.name} is: `, res);
            console.log("Function complete for cat: ", cat.name);
        } catch (error) {
            console.log(error);
            throw 'error';
        }
    });

Console logs for both unsuccessful and successful update are the following

ItemId for Cat Coffee is 620fdc8858462f33735c986

{ name: 'Coffee' } 

RES for Coffee is:  WriteResult { 
_writeTime: Timestamp { _seconds: 1645206666, _nanoseconds: 686306000 } 
} 

Function complete for cat:  Coffee 


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source