【问题标题】:UnhandledPromiseRejectionWarning: Error: Data must be an objectUnhandledPromiseRejectionWarning:错误:数据必须是对象
【发布时间】:2021-07-01 05:46:34
【问题描述】:

错误:UnhandledPromiseRejectionWarning: Error: Data must be an object

这是我的 JSON 文件 enter link to see my JSON file

我无法启动我的 json-server,因为这个 REST API:给了我一个 JSON 数组。 但我需要一个 JSON 对象来运行 json-server。 是否可以使用 JSON Array 运行 json-server?

如果是这样,请告诉我...我应该在我的 json 文件中修复什么?

npm json-server --port 3000 --watch db.json

触发此错误消息:

错误:数据必须是对象。找到对象。见 以https://github.com/typicode/json-server 为例。

我应该如何让我的 JSON 与我的 JSON 服务器一起工作?

【问题讨论】:

  • 您的 db.json 不是有效的 JSON
  • 阿达什莫汉我不懂
  • 查看文档中的module 方法
  • 阿达什莫汉能具体指出错误吗?
  • 当使用 JSON 服务器是其默认方式时,您只能使用对象。但是您可以将其配置为具有 json 数组并使用文档中提到的模块和自定义路由在每个路由上提供每个 json

标签: javascript json ajax typescript axios


【解决方案1】:

正确的 JSON 文件

correct JSON file link here

[1]: https://pastebin.com/XqC4c2HT

【讨论】:

    【解决方案2】:

    Json 对象应按以下格式发布:

       {
        "Posts":[
         {}, {}
         ]
       }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-11-20
      • 1970-01-01
      • 2020-12-31
      • 2018-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-01-01
      相关资源
      最近更新 更多