【问题标题】:Angular with json-server post entities without ID带有 json-server 的 Angular 发布实体,没有 ID
【发布时间】:2019-11-04 08:15:38
【问题描述】:

我正在使用模拟服务器 json-server 开发一个 Angular 项目。 GET 工作正常,但现在我在使用 POST 时遇到了一些问题。
我用这些数据定义了这个文件interventi.json

{
    "interventi":[
        {
            "codice": "123",
            "codiceUso": 897,
            "dataIntervento": "21-06-2019",
            "dataInserimento": "24-06-2019",
            "cooperativa": "Example",
            "puntoVendita": "Example",
            "tipiAttivita": [
                "Prelievo",
                "Sanzione"
            ],
            "organoCompetente": "Ex"
        },
        {
            "codice": "456",
            "codiceUso": 823764,
            "dataIntervento": "24-06-2019",
            "dataInserimento": "29-06-2019",
            "cooperativa": "Example 2",
            "puntoVendita": "Example 2",
            "tipiAttivita": [
                "Prelievo"
            ],
            "organoCompetente": "Ex"
        }
    ]
}

我正在尝试发布这个 json:

{  
   "codice":"a7spx",
   "codiceUso":"123",
   "dataIntervento":"2019-06-21T12:58:09.345Z",
   "cooperativa":"2",
   "puntoVendita":2,
   "tipiAttivita":[  
      "Attivita 1",
      "Attivita 2"
   ],
   "organoCompetente":3,
   "dataInserimento":"2019-06-21T12:58:09.345Z"
}

但它给了我 500 内部服务器错误:

"TypeError: 无法读取未定义的属性 'id' 在 Function.createId (C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\json-server\lib\server\mixins.js:47:39) 在 Function.insert (C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\lodash-id\src\index.js:47:49) 在 C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\lodash\lodash.js:4374:28 在 arrayReduce (C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\lodash\lodash.js:683:21) 在 baseWrapperValue (C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\lodash\lodash.js:4373:14) 在 LodashWrapper.wrapperValue (C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\lodash\lodash.js:9052:14) 在创建时(C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\json-server\lib\server\router\plural.js:225:50) 在 Layer.handle [as handle_request] (C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\express\lib\router\layer.js:95:5) 在下一个(C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\express\lib\router\route.js:137:13) 在下一个 (C:\Users\Marina\coop-workspace\wqrng\web\src\main\angular\wqrng\node_modules\express\lib\router\route.js:131:14)"

我关注this tutorial 发帖,看到id 字段不是强制性的,但服务器在抱怨。如果我在我的 json 中添加 id 字段,它可以正常工作。但我不想在我的实体中使用 id,有没有办法禁用这种行为?

编辑

// Return incremented id or uuid
// Used to override lodash-id's createId with utils.createId
function createId(coll) {
  var _ = this;
  var idProperty = _.__id();
  if (_.isEmpty(coll)) {
    return 1;
  } else {
    var id = _(coll).maxBy(idProperty)[idProperty];

    // Increment integer id or generate string id
    return _.isFinite(id) ? ++id : nanoid(7);
  }
}

【问题讨论】:

  • 发布Function.createIdcode
  • @MaihanNijat 已发布,但我无法更改,它来自 json-server 库。
  • 如果无法编辑服务器端,则检查id是否存在,如果没有在id字段中传递虚拟数据。
  • 正如我所说,如果我将 id 字段添加到我的 json 中,它会正确发布,但我不想这样做,我的实体必须没有 id。我正在寻找一种方法来禁用此限制。
  • 如果你不能改变服务器端,你就不能。要求是在服务器端而不是前端来修​​复它。

标签: json angular lodash angular7 json-server


【解决方案1】:

如果您使用除默认“id”以外的任何 id,则您已使用 json-server CLI 显式配置它。

--id, -i           Set database id property (e.g. _id)         [default: "id"]

查看以下链接

https://github.com/typicode/json-server#cli-usage

【讨论】:

  • 您好,谢谢。但是,如果我有不同实体的不同文件并且它们有不同的 id 怎么办?比如,实体“用户”有属性“名称”作为 id,实体“课程”有“代码”作为 id,等等......
猜你喜欢
  • 2019-08-24
  • 2018-11-15
  • 2011-04-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多