【问题标题】:load json data into editorjs将 json 数据加载到 editorjs
【发布时间】:2021-11-11 12:53:49
【问题描述】:

在我的网页上,我有一个 editorjs。用户输入信息,并将其保存到我的 mongodb 中(作为 JSON)。如果我想编辑信息,我会尝试从数据库中获取该信息并将其加载回 editorjs。我尝试了几种不同的方法;这些都不起作用。这就是我的 editorjs.js 文件中的内容:

const description = data.description; // My JSON that was saved.
console.log(description); // So far so good, console logs just what should be expected.

const editor = new EditorJS({
  holderID: 'editorjs',
  tools: {
    // toolbar configuration here
  },
  data: description // This doesn't work, however if I copy and paste
                    // the data that the console logged from earlier,
                    // it loads correctly into the editorjs
});

// My newest attempt here
editor.isReady.then(() => {
  console.log('ok'); // Works so far
  console.log(description); // Also works, logging the same information as earlier
  editor.render({
    description
  });
}).catch((err) => {
  console.log(err);
});

我愿意接受建议。

【问题讨论】:

  • 你试过 JSON.parse(description) 吗?
  • 我做到了,但没有奏效,但我是这样做的: const description = JSON.parse(data.description);我刚刚尝试的是有两条单独的线路,出于某种原因它起作用了。所以现在我有 var description = data.description;描述 = JSON.parse(描述);这行得通。

标签: javascript database load editorjs


【解决方案1】:

我在 cmets 中说过这个,但我会在这里说出来,以便其他人在看到这个帖子时可以更清楚地看到它。他缺少 JSON.parse() 来解析 JSON。

【讨论】:

    猜你喜欢
    • 2021-05-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    • 2018-11-17
    • 2012-07-13
    • 2015-02-01
    • 1970-01-01
    相关资源
    最近更新 更多