【问题标题】:How to update json file used for storing data for react blog app如何更新用于存储反应博客应用程序数据的 json 文件
【发布时间】:2019-08-25 11:24:02
【问题描述】:

我正在开发一个反应博客应用程序,其中列出了文章和对文章的评论。我正在使用 JSON 文件将数据存储在本地。 我添加了添加新评论的按钮。我无法将新评论添加到 JSON 数据。

我已将数据导入到我的组件文件中。我被困在如何更新 JSON 上。

[{
  "id": "5d403e5fbe39eb87471b8cd9",
  "title": "laboris duis veniam",
  "content": "Sint quis nisi tempor officia. occaecat pariatur sunt.\r\n",
  "author":
  {
    "_id": "5d403e5f552ff12f1d0773d0",
    "name": "Amanda Mckinney"
  },
  "comments": [
  {
    "id": "5d403e5f416a8a6b0447a069",
    "content": "Do minim Culpa nostrud laborum consectet\r\n",
    "commenter":
    {
      "_id": "5d403e5f58174caa22041e78",
      "name": "Angelina Wong"
    }
  },
  {
    "id": "5d403e5f17475a36a4745241",
    "content": "Velit reprehenderit et consectetur id in .\r\n",
    "commenter":
    {
      "_id": "5d403e5f3511fa317d0bf561",
      "name": "Gail Moss"
    }
  },
  {
    "id": "5d403e5f8419bad3767550b1",
    "content": "Labore veniam dolor  deserunt qui minim.\r\n",
    "commenter":
    {
      "_id": "5d403e5f28bd1b5fc4d1be1b",
      "name": "Staci Pierce"
    }
  },
  {
    "id": "5d403e5f43a52dd53b63f6c6",
    "content": "Pariatur quis proident  dolor veniam laborum.\r\n",
    "commenter":
    {
      "_id": "5d403e5f33be38b996c4a8f1",
      "name": "Mai Delaney"
    }
  },
  {
    "id": "5d403e5f9c53f6000ca12607",
    "content": "Labore reprehenderit pariatur culpa non\r\n",
    "commenter":
    {
      "_id": "5d403e5f1f77294e7fc77552",
      "name": "Geraldine Gilliam"
    }
  }]
}]

在评论会话中,我想添加带有详细信息的新评论对象,例如

{
  "id": "5d403e5f9c53f6000ca12607",
  "content": "Labore reprehenderit pariatur culpa non\r\n",
  "commenter": {
    "_id": "5d403e5f1f77294e7fc77552",
    "name": "Geraldine Gilliam"
  }
}

【问题讨论】:

    标签: json reactjs react-redux


    【解决方案1】:

    您不能在浏览器环境中写入机器的文件。您必须使用Node.js 来实现此目的。

    【讨论】:

    • 嗨,我在这里也使用 Node.js,很抱歉在问题中错过了这个
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-06
    • 2021-05-03
    • 2020-03-05
    • 1970-01-01
    • 1970-01-01
    • 2014-08-27
    相关资源
    最近更新 更多