我正在使用模拟服务器json-server开发一个有角度的项目。 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 [作为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);
}
}
答案 0 :(得分:1)
如果您使用的是其他ID(而非默认ID),则可以使用json服务器CLI对其进行显式配置。
--id, -i Set database id property (e.g. _id) [default: "id"]
检查以下链接