Create code for automations to add order number to collection

I want to add the order number to the right row in my collection to connect it to other data from the website.
My code does not seem to work however. It’s supposed to look for the cartId, which is saved in the collection earlier already, and match it with the one extracted from the order.
Then add the order number in the orderNumber field.
Please have a look:

import wixData from 'wix-data';

// This is the function that Wix Automation will call
export async function invoke(context) {
    console.log("Invoke function called. Context:", JSON.stringify(context, null, 2));

    // Extract payload data from `context`.
    // The exact structure depends on how you've configured your custom integration.
    // Usually, the arguments are found in `context.arguments`.
    const payload = context.arguments && context.arguments[0];

    if (!payload) {
        throw new Error("No payload received in invoke function.");
    }

    // Now call your logic to update the order number
    return await updateOrderNumberFromOrderPayload(payload);
}

// Your existing logic to update the order number
async function updateOrderNumberFromOrderPayload(payload) {
    console.log("updateOrderNumberFromOrderPayload called with payload:", JSON.stringify(payload, null, 2));

    const { orderNumber, cartId } = payload;

    if (!orderNumber || !cartId) {
        console.error('Missing orderNumber or cartId in payload');
        throw new Error('Missing orderNumber or cartId in payload');
    }

    console.log(`Querying StickerDesigns with cartId: ${cartId}`);
    const queryResult = await wixData.query('StickerDesigns')
        .eq('cartId', cartId)
        .find();

    if (queryResult.items.length === 0) {
        console.error(`No item found in StickerDesigns with cartId: ${cartId}`);
        throw new Error(`No item found in StickerDesigns with cartId: ${cartId}`);
    }

    const itemToUpdate = queryResult.items[0];
    console.log('Found item to update:', JSON.stringify(itemToUpdate, null, 2));

    itemToUpdate.orderNumber = orderNumber;
    console.log(`Updating item with orderNumber: ${orderNumber}`);

    const updateResult = await wixData.update('StickerDesigns', itemToUpdate);
    console.log(`Successfully updated orderNumber for cartId: ${cartId}`, JSON.stringify(updateResult, null, 2));

    return { success: true, updatedItem: updateResult };
}

Your code logic looks solid overall, but there are a few potential issues to address. Here’s a refined version with corrections and suggestions:
import wixData from ‘wix-data’;

// This is the function that Wix Automation will call
export async function invoke(context) {
console.log(“Invoke function called. Context:”, JSON.stringify(context, null, 2));

// Extract payload data from `context`
const payload = context.arguments && context.arguments[0];

if (!payload) {
    throw new Error("No payload received in invoke function.");
}

// Call your logic to update the order number
try {
    return await updateOrderNumberFromOrderPayload(payload);
} catch (error) {
    console.error("Error updating order number:", error.message);
    throw error; // Re-throw the error for Wix Automation to handle
}

}

// Logic to update the order number
async function updateOrderNumberFromOrderPayload(payload) {
console.log(“updateOrderNumberFromOrderPayload called with payload:”, JSON.stringify(payload, null, 2));

const { orderNumber, cartId } = payload;

// Validate payload data
if (!orderNumber || !cartId) {
    console.error("Missing orderNumber or cartId in payload.");
    throw new Error("Missing orderNumber or cartId in payload.");
}

console.log(`Querying StickerDesigns with cartId: ${cartId}`);
const queryResult = await wixData.query('StickerDesigns')
    .eq('cartId', cartId)
    .find();

// Handle case where no item is found
if (queryResult.items.length === 0) {
    console.error(`No item found in StickerDesigns with cartId: ${cartId}`);
    throw new Error(`No item found in StickerDesigns with cartId: ${cartId}`);
}

// Update the first matching item
const itemToUpdate = queryResult.items[0];
console.log('Found item to update:', JSON.stringify(itemToUpdate, null, 2));

itemToUpdate.orderNumber = orderNumber;
console.log(`Updating item with orderNumber: ${orderNumber}`);

// Update the database
try {
    const updateResult = await wixData.update('StickerDesigns', itemToUpdate);
    console.log(`Successfully updated orderNumber for cartId: ${cartId}`, JSON.stringify(updateResult, null, 2));
    return { success: true, updatedItem: updateResult };
} catch (updateError) {
    console.error("Error updating database:", updateError.message);
    throw new Error("Failed to update order number in the database.");
}

}