【问题标题】:How to use editor.js with mongodb如何在 mongodb 中使用 editor.js
【发布时间】:2021-11-10 23:32:42
【问题描述】:

我正在用 nextjs 建立一个博客。我正在使用 editor.js 作为我的编辑器,但我发现很难将 editor.js 数据保存在 mongodb 数据库中

我的博客模型:

const blogSchemma = new mongoose.Schema(
  {
    title: {
      type: String,
      required: true,
    },
    description: {
      type: String,
      required: true,
      maxLength: [120, "Room name cannot exceed 100 characters"],
    },
    post: {
      type: String,
      // required: true,
    },
    images: [
      {
        public_id: {
          type: String,
          // required: true,
        },
        url: {
          type: String,
          // required: true,
        },
      },
    ],
  },
  { timestamps: true }
);

module.exports = mongoose.models.Blog || mongoose.model("Blog", blogSchemma);

我试图将博客文章保存到数据库的位置:

    const [title, setTitle] = useState("");
      const [description, setDescription] = useState("");
      const [post1, setPost1] = useState("");
      const [introduction, setIntroduction] = useState("");
  const [images, setImages] = useState([]);


  const onSaveHandler = async (editorInstance) => {
      const post = await editorInstance.save();
      if (!title || title === "")
        throw new Error("Title cannot be empty. Please enter title");
      if (!post.blocks[0])
        throw new Error("Blog cannot be empty. Please enter some data");

      const blog = {
        title,
        description,
        images,
        post,
      };
      dispatch(createBlog(blog));
   
  };



 let blogContent;
  if (!editorTools) blogContent = <p>loading...</p>;
  else
    blogContent = (
      <EditorJs
        instanceRef={(instance) => (editorInstance = instance)}
        tools={editorTools}
        // readOnly={readOnly}
        data={post1}
        placeholder={`Let's write an awesome blog!`}
      />
    );

我遇到的错误

消息:“值“{\n 时间:1631742225097,\n 块:[ { id:'gKAdY5E37a',类型:'段落',数据:[Object] }],\n 版本:转换为字符串失败: '2.22.2'\n}" (类型 Object) 在路径 "post""

【问题讨论】:

  • 我想你应该 JSON.stringify() 描述(或任何编辑器状态)。 editor.js 将返回一个对象而不是 (HTML) 字符串

标签: reactjs next.js editorjs


【解决方案1】:

问题出在博客模型上 我从这里改变了:

  post: {
      type: String,
      // required: true,
    },

到这里:

post: { type: mongoose.Schema.Types.Mixed }

【讨论】:

    猜你喜欢
    • 2019-10-02
    • 2023-02-14
    • 1970-01-01
    • 2023-01-23
    • 2022-08-09
    • 2012-05-12
    • 2013-06-29
    • 2014-07-13
    • 1970-01-01
    相关资源
    最近更新 更多