0

我正在用 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',类型:'paragraph',数据:[Object]}],\n 版本:'2.22。 2'\n}" (类型 Object) 在路径 "post""

4

1 回答 1

0

问题来自我从这个改变的博客模型:

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

对此:

post: { type: mongoose.Schema.Types.Mixed }
于 2021-09-16T16:36:04.207 回答