[go: up one dir, main page]

Skip to content

Comentario JS hard fails if there's a broken commenter data

We have a number of invalid commenter objects in our comentario v1 dump which lead to creation of users that apparently are broken in the end comentario data model. They have no name. Then when comentario JS is being rendered the following error is raised:

comentario.js:1 Uncaught (in promise) TypeError: Cannot read properties of undefined (reading '0')
    at HTMLElement.createAvatarElement (comentario.js:1:85217)
    at Object.onGetAvatar (comentario.js:1:84429)
    at _t.render (comentario.js:1:44934)
    at new _t (comentario.js:1:43595)
    at comentario.js:1:43854
    at Array.map (<anonymous>)
    at _t.renderChildComments (comentario.js:1:43846)
    at HTMLElement.renderComments (comentario.js:1:75715)
    at HTMLElement.<anonymous> (comentario.js:1:75096)
    at Generator.next (<anonymous>)

The root cause is the default switch case with t.name[0] access where t.name is undefined:

        createAvatarElement(t) {
            switch (!0) {
            case !t:
                return m.div("avatar", "bg-deleted");
            case t.id === d:
                return m.div("avatar", "bg-anonymous");
            case t.hasAvatar:
                return u.new("img").classes("avatar-img").attr({
                    src: this.apiService.getAvatarUrl(t.id, this.avatarSize),
                    loading: "lazy",
                    alt: ""
                });
            default:
                return m.div("avatar", `bg-${t.colourIndex}`).html(t.name[0].toUpperCase())
            }
        }

Here's an example object we get from the comentario server:

{
    "colourIndex": 34,
    "createdTime": "0001-01-01T00:00:00.000Z",
    "hasAvatar": false,
    "id": "87d7dd6e-251f-4238-896e-41d05a8cf41d",
    "isCommenter": true,
    "isModerator": false
}

weirdly, the commenter object is accessible via the comentario UI and user's comments are rendered.

Edited by Dmitry Kann