ckeditor5-vue icon indicating copy to clipboard operation
ckeditor5-vue copied to clipboard

TypeError: Cannot read property 'setData' of undefined

Open masoudline opened this issue 4 years ago • 5 comments

how can i save editorData as a prop in vuejs? i have a laravel backend and i need send product->description as props to ckeditor my code: mounted() { if(this.mainData){ this.editorData = this.mainData } }, but i have these error: [Vue warn]: Error in callback for watcher "value": "TypeError: Cannot read property 'setData' of undefined"

masoudline avatar Nov 05 '20 07:11 masoudline

Hi, thanks for the report. Could you please provide some more precise steps + code to reproduce this error? Also, does this error occur only when trying to set data taken from the server, and not with other content?

FilipTokarski avatar Nov 06 '20 12:11 FilipTokarski

For anyone coming across this in future; there seems to be an issue with assigning a value to ckeditor in the 'mounted' hook. Not 100% sure on what's causing this but it seems it's trying to assign the value before the ckeditor instance has been created; a workaround is to use the following to delay the assignment:

setTimeout(() => {
    this.editorValue = 'New Value';
}, 100);

ConnorHowell avatar Nov 20 '20 14:11 ConnorHowell

@ConnorHowell the setTimeout worked. thank you for saving me hours of frustrations. however, the 100ms is a bit arbitrary for me. all we are doing is waiting for the editor instance to be ready. so moving the value assignment into the 'ready' event also did the same thing.

<ckeditor @ready="onEditorReady" v-model="editorValue"></ckeditor>

in methods

methods: {
    onEditorReady: function() { this.editorValue = 'New Value'; }
}

bizsimon avatar Dec 10 '20 05:12 bizsimon

@bizsimon good point, guessing the permanent fix to this issue would be for the component to check the instance exists prior to assigning the value in the watcher for 'modelValue': https://github.com/ckeditor/ckeditor5-vue/blob/3ee73dfc649cf7c27a1c5415eeb9f3c34f5e7f90/src/ckeditor.js#L123-L125

@pomek do you want a PR for this, seems like a fairly trivial fix?

ConnorHowell avatar Dec 10 '20 11:12 ConnorHowell

I have created a component that uses the CKEditor inside

<template> <ckeditor :editor="editor" v-model="editorValue" @ready="editorReady" :config="configurations" :disabled="disabled"></ckeditor> </template>

editorValue is bean updated from the watcher: watch: { editorValue(newText) { this.$emit('input', newText) }, value(newValue){ this.editorValue = newValue } }

this was triggering the error when the component was accessed for second time so i solved it by including 2 new variables: ready and tempValue

the editorReady become: editorReady(){ this.ready = true this.editorValue = this.tempValue }

the watcher changes to: watch: { editorValue(newText) { this.$emit('input', newText) }, value(newValue){ if(this.ready){ this.editorValue = newValue } else { this.tempValue = newValue } } }

so instead of using a setTimeout i change the value of the ckeditor when the editor is ready. hope this will help someone

cerberos avatar Mar 09 '21 16:03 cerberos