Datatable Error when setting the columns

Hello, i got a datatable component on my form. It has 7 columns: Category, CurrentLimitCZ, LimitCZ, CurrentLimitSK, LimitSK, CurrentLimitPL, LimitPL. I fetch a Json(multiple line text) from list and show current limits in datatable if the category match. The current limits shows in a datatable, but i got a problem when i click into some column and then try to click into another one i got this error.

Here is my js code for datatable:

function setCurrentLimitsInNewLimitsTable() {
    const newLimitsTable = fd.control('DataTable1');
    if (!newLimitsTable) return;

    pnp.sp.web.lists.getByTitle("Limits").items
        .select("JsonMSR")
        .top(1)
        .get()
        .then(function(items) {
            if (items.length === 0) return;

            const currentLimitsJson = items[0].JsonMSR;
            let currentLimits = [];

            try {
                currentLimits = JSON.parse(currentLimitsJson);
            } catch (e) {
                console.error("Invalid JSON in current limits:", e);
                return;
            }

            const tableRows = newLimitsTable.value.map(row => {
                const matched = currentLimits.find(c => c.Category === row.Category);
                return {
                    ...row,
                    CurrentLimitCZ: matched?.LimitCZ || 0,
                    CurrentLimitSK: matched?.LimitSK || 0,
                    CurrentLimitPL: matched?.LimitPL || 0,
                    LimitCZ: 0,
                    LimitSK: 0,
                    LimitPL: 0
                };
            });

            newLimitsTable.value = tableRows;
        })
        .catch(function(error) {
            console.error("Error fetching limits:", error);
        });
}

fd.spRendered(function () {
    
    setTimeout(setCurrentLimitsInNewLimitsTable, 100);
...rest of another code

Thank you in advance.

Dear @Marek,
How does it work again? I see that you try to get limits by Category, but the code is only running once, when the form is rendered, where are the Category values coming from? Does this code run on change as well, or are the Category values somehow populated otherwise?

We might need more information, and any other code that could be related to the same Data Table - I really want to know where the category comes from, and if there's any code running on change event as well.

Of course. I got the current limits from Json that looks like this:
image
I want to get the data from the Json to CurrentLimitCZ, CurrentLimitSk and CurrentLimitPL only when the form is loaded. I dont want to update this or anything, just to be shown there. That works well.


But when i try to update lets say Limit CZ(CZK) i got the error i posted before.

Thank you

Dear @Marek,
So what's the logic here, should the values be automatically set only when Category is selected?

If I understand what you want to achieve, it's the following:

  1. When the form opens, send a request to the Limits list, and get the JSON values
  2. When user starts adding values, automatically set limits based on values in JSON, right?

If the above is true, should the limits be changable, or are they static?

Because right now you're doing something else, and I want to make sure I understand the required functionality - so let me know if I didn't get anything right.