BorisMoore / jsrender

A lightweight, powerful and highly extensible templating engine. In the browser or on Node.js, with or without jQuery.
http://www.jsviews.com
MIT License
2.68k stars 340 forks source link

Uncaught TypeError: Cannot read property 'tag' of undefined (line 1381) #317

Closed martrix78 closed 7 years ago

martrix78 commented 7 years ago

Hey. Sometimes when I reload the page, I see this error. Uncaught TypeError: Cannot read property 'tag' of undefined The error does not appear after updating the page. What can be done?

BorisMoore commented 7 years ago

I don't know - it could be due to your code, so I would need to have more context. Can you create a jsfiddle which reproduces the issue? Otherwise, at a minimum you need to debug - break on all exceptions, or on uncaught exceptions, and show the stack trace. And also show your own code calling JsRender. Are you using the render method (JsRender) or the link method (JsViews)?

martrix78 commented 7 years ago

I run the renderer like this: $('#someId .js-feedback').html( $( '#feedbackTemplate ).render( data));

BorisMoore commented 7 years ago

And what is the template markup in your js_template?

martrix78 commented 7 years ago
<script id="feedbackTemplate" type="text/html">
    <li  id="faq-{{:id}}" data-tree-id='' class="comments__item">
        <div class="comments__body">
            <div class="comments__head">
                <div class="comments__user">
                    <div class="userbox userbox_comment">
                        <div class="userbox__item">
                            <div class="userbox__icon">
                                <a href="{{:author.link}}"><img src="{{:author.avatar}}"></a>
                            </div>
                        </div>
                        <div class="userbox__item">
                            <div class="userbox__sub"><a href="#" class="userbox__link">{{:author.name}}</a>
                                <time class="userbox__time hidden-xs">{{:date}}</time>
                            </div>
                            <div class="userbox__sup js-hide-on-edit" id="no-edit-label-{{:id}}">{{:from}}</div>
                            <div id="edit-label-{{:id}}" class="userbox__sup js-show-on-edit" style="display: none;" >Редактирование вопроса</div>
                        </div>
                        <div class="userbox__item hidden-xs js-hide-on-edit">
                            {{if status == 1 && userIsOwner}}<div class="status status_moderate">На модерации</div> {{/if}}
                            {{if status == 3 && userIsOwner}}<div class="status status_publish">Опубликован</div> {{/if}}
                        </div>
                    </div>
                    <time class="comments__time visible-xs">{{:date}}</time>

                </div>
                <div class="comments__action">
                    <div class="comment-button">
                        {{if userIsOwner && !read_only}}
                        <a href="#" data-comment-id="{{:id}}" class="js-action-link comment-button__icon comment-button__icon_edit js-action-link js-faq-link-edit js-comment-link-edit">
                            <svg><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/_source/assets/images/icon.svg#icon_edit-post"></use></svg>
                        </a>
                        <a href="#" data-url="/api/comment/delete/?id={{:id}}&type=faq" data-id="{{:id}}" class="js-action-link js-feedback-del comment-button__icon comment-button__icon_delete js-faq-link-delete">
                            <svg><use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="/_source/assets/images/icon.svg#icon_delete-post"></use></svg>
                        </a>
                        {{/if}}
                        {{if userIsConsultant && !read_only}}
                            <a href="javascript:void(0);" onClick="reply({{:id}});" class="js-action-link comments__do-replay js-action-link ">Ответить</a>
                        {{/if}}
                    </div>
                </div>
                <div class="comments__status visible-xs js-hide-on-edit">
                    {{if status == 1}}<div class="status status_moderate">На модерации</div> {{/if}}
                    {{if status == 3 && userIsOwner}}<div class="status status_publish">Опубликован</div> {{/if}}
                </div>
            </div>
            <div class="comments__text js-hide-on-edit js-faq-content">
                {{:content}}
            </div>
            {{if userIsOwner && !read_only}}
            <div class="comments__text js-show-on-edit" style="display: none;" id ='comments-form-{{:id}}'>
                     <form id="commentform-{{:id}}" class="edit-comment" action="/api/comment/update-comment/?id={{:id}}&entity={{:entity}}&type=faq" method="post" data-id="{{:id}}">
                         <input id="comment-user_id-{{:id}}" class="form-control" name="ConsultationQuestion[user_id]" value="{{:author.id}}" type="hidden">
                         <textarea id="comment-content-{{:id}}" class="textarea-control comments__textarea-ctrl" name="ConsultationQuestion[question]" rows="3" placeholder="" data-comment="content" data-emojiable="true"  >{{:contentToEdit}}</textarea>

                        <div class="comments__buttons">
                            <a href="javascript:void(0)" onclick="cancelEditComment('{{:id}}')" class="js-action-link  btn-cancel-comment button button_as-link" title="">
                                Отмена
                            </a>
                            <a href="javascript:void(0)" onclick="submitForm('commentform-{{:id}}')" class="js-action-link button  btn-send-edit-comment edit-comment send-comment button button_save-comment" title="">
                                Сохранить
                            </a>
                        </div>
                     </form>
                    </div>
            </div>
            {{/if}}
        </div>
    </li>
    <li id="faq-answer-{{:id}}"  class="comments__item comments__item_child" {{if !answer.content}} style="display:none;"{{/if}}>
        <div class="comments__body">
            <div class="comments__head">
                <div class="comments__user">
                    <div class="userbox userbox_comment">
                        <div class="userbox__item">
                            <div class="userbox__icon">
                                <a href="{{:answer.author.link}}"><img src="{{:answer.author.avatar}}"></a>
                            </div>
                        </div>
                        <div class="userbox__item">
                            <div class="userbox__sub"><a href="#" class="userbox__link">{{:answer.author.name}}</a>
                                <time class="userbox__time hidden-xs">{{:answer.date}}</time>
                            </div>
                            <div class="userbox__sup js-hide-on-edit" id="no-edit-label-{{:id}}">консультант</div>
                            <div id="edit-label-{{:id}}" class="userbox__sup js-show-on-edit" style="display: none;" >Ответ пользователю {{:author.name}}</div>
                        </div>

                    </div>
                    <time class="comments__time visible-xs">{{:answer.date}}</time>

                </div>
                <div class="comments__action">
                    <div class="comment-button">
                    </div>
                </div>
                <div class="comments__status visible-xs js-hide-on-edit">
                </div>
            </div>
            <div class="comments__text js-hide-on-edit js-faq-content">
                {{:answer.content}}
            </div>
            {{if !read_only }}
            <div class="comments__text js-show-on-edit" style="display: none;" id ='comments-form-{{:id}}'>
                     <form id="replyform-{{:id}}" class="edit-comment" action="/api/comment/create/?entity={{:entity}}&type=faq-reply" method="post" data-id="{{:id}}">
                         <input id="comment-user_id-{{:id}}" class="form-control" name="ConsultationReply[consultation_question_id]" value="{{:id}}" type="hidden">
                         <textarea id="comment-content-{{:id}}" class="textarea-control comments__textarea-ctrl" name="ConsultationReply[reply]" rows="3" placeholder="" data-comment="content" data-emojiable="true"  >{{:answer.reply}}</textarea>

                        <div class="comments__buttons">
                            <a href="javascript:void(0)" onclick="$('#faq-answer-{{:id}}').hide();" class="js-action-link  btn-cancel-comment button button_as-link" title="">
                                Отмена
                            </a>
                            <a href="javascript:void(0)" onclick="submitForm('replyform-{{:id}}')" class="js-action-link button  btn-send-edit-comment edit-comment send-comment button button_save-comment" title="">
                                Сохранить
                            </a>
                        </div>
                     </form>
                    </div>
            </div>
            {{/if}}
        </div>
    </li>
</script>
martrix78 commented 7 years ago

<ul class=" js-feedback"></ul>

BorisMoore commented 7 years ago

There are two unmatched </div> closing tags. (after </form>).

If fixing that doesn't solve the issue, I would need a stack trace... I'm not sure what you mean by reloading the page and updating the page. What is the difference between those actions? How are you reloading/updating?

martrix78 commented 7 years ago

First, I open the page by reference, if an error occurs, then press f5. I understand that this sounds stupid, but with the same raw data, the error sometimes appears that disappears.

BorisMoore commented 7 years ago

OK - well you'll need to open a debugger (F12 in chrome, for example) and do some testing, to get more information and understanding, and a stack trace... Also, clear the browser cache completely and see if it still happens...

martrix78 commented 7 years ago

I was sent such a screenshot http://joxi.ru/RmzYkKwU9pnVrO

BorisMoore commented 7 years ago

I wonder if you (or your system) are doing something to modify the jQuery cached data on the feedbackTemplate element. See issue, https://github.com/BorisMoore/jsrender/issues/310, which could be related.

If the jQuery cached template $.data(feedbackTemplateScriptElement ).jsvTmpl is getting removed, it can cause that error you are seeing.

Can you try loading this patched version of jsrender, jsrender.zip and let me know if it fixes the issue, for you? (But still it is better not to ever clear the cached template, since it will have better performance to use the cached copy).

BorisMoore commented 7 years ago

The defensive code against corrupted jQuery cached template on the script container element is included in commit 85. Closing this issue.