我一直在尝试创建一个包含回送对象数组的集合。

我想要一个这样的商店数据格式:

{
  id: "16356135616aaasad", //autogenerated by mongo
  "name" : "a name",
  "valores": [
    {"valor": 567, "fecha": "2016-08-18T00:00:00.000Z"},
    {"valor": 569, "fecha": "2016-08-19T00:00:00.000Z"},
    ...
  ]
}


我在环回中具有以下配置:

indicador.json

{
  "name": "Indicador",
  "plural": "indicadores",
  "base": "PersistedModel",
  "idInjection": true,
  "options": {
    "validateUpsert": true,
    "autoId": true
  },
  "properties": {
    "nombre": {
      "type": "string",
      "required": true
    }
  },
  "relations": {
    "historico": {
      "type": "embedsMany",
      "model": "Valor"
    }
  }
}


这是与另一个模型关联的基础集合(不是持久性的)
valor.json

{
  "name": "Valor",
  "plural": "valores",
  "base": "Model",
  "properties": {
    "valor": {
      "type": "number",
      "required": true
    },
    "fecha": {
      "type": "date",
      "required": true
    }
  }
}


问题是当我尝试将帖子发送到端点时。如果我发送此数据

{
  "nombre": "UF",
  "valores": [
    {
      "valor": 0,
      "fecha": "2016-08-18"
    }
  ]
}


API对此进行响应:

{
  "error": {
    "name": "ValidationError",
    "status": 422,
    "message": "The `Indicador` instance is not valid. Details: `valores` contains invalid item at index `0`: `id` is blank (value: [ { valor: 0, fecha: 2016...} ]).",
    "statusCode": 422,
    "details": {
      "context": "Indicador",
      "codes": {
        "valores": [
          "invalid"
        ]
      },
      "messages": {
        "valores": [
          "contains invalid item at index `0`: `id` is blank"
        ]
      }
    },
    "stack": "ValidationError: The `Indicador` instance is not valid. Details: `valores` contains invalid item at index `0`: `id` is blank (value: [ { valor: 0, fecha: 2016...} ]).\n    at /home/dev/app/node_modules/loopback-datasource-juggler/lib/dao.js:322:12\n    at ModelConstructor.<anonymous> (/home/dev/app/node_modules/loopback-datasource-juggler/lib/validations.js:492:11)\n    at ModelConstructor.next (/home/dev/app/node_modules/loopback-datasource-juggler/lib/hooks.js:81:12)\n    at ModelConstructor.<anonymous> (/home/dev/app/node_modules/loopback-datasource-juggler/lib/validations.js:489:23)\n    at ModelConstructor.trigger (/home/dev/app/node_modules/loopback-datasource-juggler/lib/hooks.js:71:12)\n    at ModelConstructor.Validatable.isValid (/home/dev/app/node_modules/loopback-datasource-juggler/lib/validations.js:455:8)\n    at /home/dev/app/node_modules/loopback-datasource-juggler/lib/dao.js:318:9\n    at doNotify (/home/dev/app/node_modules/loopback-datasource-juggler/lib/observer.js:98:49)\n    at doNotify (/home/dev/app/node_modules/loopback-datasource-juggler/lib/observer.js:98:49)\n    at doNotify (/home/dev/app/node_modules/loopback-datasource-juggler/lib/observer.js:98:49)"
  }
}


¿为什么我会收到此错误?

我不需要数组对象内的id。我不明白为什么会这样。

最佳答案

您需要指定嵌入式模型不需要id

"relations": {
    "historico": {
      "type": "embedsMany",
      "model": "Valor",
      "options": {
        "forceId": false,
        "validate": true,
        "persistent": false
      }
    }
  }

07-24 09:30