Uncaught TypeError: Cannot read properties of undefined (reading 'getBody()')

Viewed 19

CKEditor Ver.4 This is a cshtml file.

I try to get two kind of content types (with html tags & without one). But it seems not work that.

Error Msg:
Uncaught TypeError: Cannot read properties of undefined (reading 'getBody')

  • HTML code:
<textarea class="edit-normal" id="content" msg="content" disabled="disabled">@Html.Raw(Model.Data.Content)</textarea>
  • JS code
function GetData() {
    // TODO: Get content with html tags ✓
    var contentEditor = CKEDITOR.replace('content');
    var content = contentEditor.getData();

    // TODO: Get content without html tags
    var rawContent = contentEditor.document.getBody().getText();
}
1 Answers

I think you'll have to have a fallback if the getBody() is undefined. Some cases can be solved by using a questionMark(?) before the param you're trying to show.

Possibly add the || {} at the end of the rawContent snippet.

Related