CKEditor报错:Cannot convert undefined or null to object 问题求助
CKEditor5 "Cannot convert undefined or null to object" 错误排查及修复
问题根源
你的代码中,useEffect会将data?.desc赋值给mainDesc,当data?.desc为null或undefined时,直接将其传给CKEditor的data属性会触发错误——CKEditor5要求data必须是字符串类型,无法处理null/undefined值。
修复方案
有两种简单的修复方式:
- 在状态赋值时兜底
修改useEffect中的逻辑,确保mainDesc始终为字符串:
useEffect(() => { setMainDesc(data?.desc || ''); }, [data]);
- 在CKEditor组件传参时兜底
直接在组件的data属性中处理,避免传递非法值:
<CKEditor editor={ClassicEditor} data={mainDesc || ''} onChange={handleMainDesc} />
原理说明
CKEditor5内部会对传入的data值进行对象化处理,当值为null或undefined时,转换逻辑会抛出"Cannot convert undefined or null to object"错误。通过将兜底值设为空字符串,保证传入的始终是合法的字符串类型,即可解决该问题。
内容的提问来源于stack exchange,提问作者Mohak Maheshwari
相关产品推荐
相关产品推荐

