【问题标题】:Draft.js - Unable to get data from the database. Cross-origin errorDraft.js - 无法从数据库中获取数据。跨域错误
【发布时间】:2021-02-27 05:00:34
【问题描述】:

我正在尝试在我正在创建的博客应用程序中使用 Draft.js。将数据保存到数据库并将其取回时,一切似乎都很好,只是我似乎无法让 createWithContent 工作。

当我编写以下内容时,数据确实会保存到数据库中,因为我只是创建一个空编辑器,然后由用户更新并在 convertToRaw 之后发送到数据库。

const postDetails = useSelector((state) => state.postDetails);
const { loading, post, error } = postDetails; 
const editorContent = EditorState.createEmpty();

const [editorState, setEditorState] = useState({ editorState: editorContent });

const handleEditorChange = (editorState) => { setEditorState({ editorState }) }

const submitHandler = (e) => {
    e.preventDefault();
    dispatch(
      updatePost({
        _id: id,
        title,
        image,
        images,
        paragraph: JSON.stringify(
          convertToRaw(editorState.editorState.getCurrentContent())
        ),
      })
    );
  };

<Editor
   editorState={editorState.editorState}
   onEditorStateChange={handleEditorChange}
 />

但是当我使用时

EditorState.createWithContent(convertFromRaw(JSON.parse(post.paragraph)))

这是我真正想要的(用来自数据库的 post.paragrah 填写的段落),我收到以下错误:

引发了跨域错误。 React 无法访问 开发中的实际错误对象。

我以这种方式处理跨域:

app.use((req, res, next) => {
  res.setHeader('Access-Control-Allow-Origin', '*'); 
  res.setHeader('Access-Control-Allow-Headers', 'Origin, X-Requested-With, Content, Accept, Content-Type, Authorization'); 
  res.setHeader('Access-Control-Allow-Methods', 'GET, POST, PUT, DELETE, PATCH, OPTIONS'); 
  next();
});

另外,当我使用 console.log(post.paragraph) 时,我确实得到了我的段落:

但我注意到,在收到我的段落之前我也得到了 2 个未定义,这可能是问题的一部分。

为了解决这个问题,我已经尝试了这两种方法:

const editorContent =   post ?
    EditorState.createWithContent(convertFromRaw(JSON.parse(post.paragraph))) : 
    EditorState.createEmpty();
    
    const [editorState, setEditorState] = useState({ editorState: editorContent });

还有这个:

const editorContent = await post.paragraph;
          if (post.paragraph) {
            res.send({ editorContent: EditorState.createWithContent(convertFromRaw(JSON.parse(post.paragraph))) });
          } else {
              res.send({ editorContent: EditorState.createEmpty()});
            }
            return editorContent;
          })
    
    
        const [editorState, setEditorState] = useState({ editorState: content });

我在网络上做了很多研究,但我没有成功地想出任何东西。 希望大家能帮忙。提前致谢

【问题讨论】:

  • devtools 的控制台或网络选项卡中应该有警告或错误,请检查它们并将该信息添加到您的问题中
  • 您好 dieu,感谢您的帮助。我确实在我的问题中添加了错误:引发了跨域错误。 React 无法访问开发中的实际错误对象。这还不够吗?谢谢
  • 在解析 JSON 时似乎发生了主要错误,记录您尝试解析的 json 字符串以查看它是否有效
  • 如果是这种情况,只需将那部分代码包装在 if if (post.paragraph) EditorState.createWithContent(... 中,因为您没有指定获取帖子数据的位置和方式,我猜可能发生了一些异步操作
  • 不,很遗憾。希望好心人来帮忙。我暂时使用降价作为临时替代方案。如果您找到解决问题的方法,请告诉我。谢谢

标签: reactjs mongodb redux text-editor cross-origin-read-blocking


【解决方案1】:

主要问题是当您尝试解析 JSON 内容时,

JSON.parse(post.paragraph)

内容未定义。这就是您收到错误的原因。在加载数据之前,您不应该呈现内容。我的具体问题如下:

const BlogPostPage: React.FC<MatchProps> = (props: MatchProps) => {

const classes = useStyles();

const { data, loading, error } = useGetBlogQuery({
    variables: {
        id: props.match.params.id
    }
});


return (
    <BlogPostContent markdown={data?.blog?.contentJson}></BlogPostContent>
);
}

在这段代码中,我通过 apollo 客户端调用异步查询。但是,在渲染调用中,我遇到了与您相同的错误。然后,我添加了以下代码,以等待数据加载。加载数据后,触发钩子并重新渲染组件。

    if (loading) return (<>{"loading..."}</>);

【讨论】:

  • 嗨,我尝试添加类似这样的内容:const editorContent = loading ? (&lt;LoadingBox /&gt;) : !loading ? EditorState.createWithContent(convertFromRaw(JSON.parse(post.paragraph))) : EditorState.createEmpty(); 但这并没有解决问题
  • 也许 post.paragraph 为空/空。也请检查一下。
  • 从我上面发布的最后一张截图中可以看出,它不是空的。我确实在 console.log 中找回了它,但在 2 次未定义之后
  • 是的,这就是我在评论中所说的。如果内容未定义,则不应渲染内容,这就是问题所在。
  • 什么意思?为什么它是未定义的? post 段落确实存在于数据库中,如果我删除 draft.js,我会毫无问题地收到它。通过添加 draft.js 并因此必须从 raw (draft.js 块)转换然后解析,我得到了问题。知道如何解决吗?
猜你喜欢
  • 2018-02-21
  • 1970-01-01
  • 2018-12-17
  • 2017-09-21
  • 2021-02-26
  • 1970-01-01
  • 2018-05-28
相关资源
最近更新 更多