alpine: Regression in x-for

This error came after 9e91c4704638ee5c3472eb8a19d54fcd0b5caa59

I don’t have a codepen for this, but the component that fails has 3 nested x-for loops.

alpine.js:418 Uncaught TypeError: Cannot convert undefined or null to object
    at alpine.js:418
    at Proxy.forEach (<anonymous>)
    at handleForDirective (alpine.js:404)
    at alpine.js:1495
    at Array.forEach (<anonymous>)
    at Component.resolveBoundAttributes (alpine.js:1450)
    at Component.updateElement (alpine.js:1426)
    at alpine.js:1394
    at alpine.js:1358
    at walk (alpine.js:85)

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Feel free to review ^

With such a deliberate function name as lookAheadForMatchingKeyedElementAndMoveItIfFound I would assume that the logical flaw is the handling of the (non) return value. That also matches the functional behaviour (it works, but it throws).

i went through and collect some information that might help you to figure out faster. nextEl = lookAheadForMatchingKeyedElementAndMoveItIfFound(nextEl, currentKey) at this line it’s assigned to undefined because:

function lookAheadForMatchingKeyedElementAndMoveItIfFound(nextEl, currentKey) {
    // If the the key's DO match, no need to look ahead.
    if (nextEl.__x_for_key === currentKey) return nextEl

    // If the don't, we'll look ahead for a match.
    // If we find it, we'll move it to the current position in the loop.
    let tmpNextEl = nextEl

    while(tmpNextEl) {
        if (tmpNextEl.__x_for_key === currentKey) {
            return tmpNextEl.parentElement.insertBefore(tmpNextEl, nextEl)
        }

        tmpNextEl = (tmpNextEl.nextElementSibling && tmpNextEl.nextElementSibling.__x_for_key !== undefined) ? tmpNextEl.nextElementSibling : false
    }
}

lookAheadForMatchingKeyedElementAndMoveItIfFound function spouse to find an element in the end but while loop fails to return because tmpNextEl in the end becomes false and it returns nothing. So nextEl becomes undefined.

Just for testing, i return back the nextEl at very end of the function like so:

function lookAheadForMatchingKeyedElementAndMoveItIfFound(nextEl, currentKey) {
...
...
...
        tmpNextEl = (tmpNextEl.nextElementSibling && tmpNextEl.nextElementSibling.__x_for_key !== undefined) ? tmpNextEl.nextElementSibling : false
    }
    return nextEl
}

and Append and Prepend worked normal.

So i guess it might be a logic issue?

update something went wrong here : https://youtu.be/qkjKSZV9csA?t=7681

@HugoDF I won’t have much time today. Feel free to fix it.