如何使用Loopback.js将模型与嵌入式模型一起发布?

时间:2015-02-01 11:30:55

标签: loopbackjs strongloop

这是表单模型架构:

{
  "startTime": "",
  "stopTime": "",
  "id": "objectid",
  "formQuestions": [
    {
      "type": 0,
      "label": "",
      "content": [
        ""
      ],
      "id": "objectid"
    }
  ]
}

form.json的关系:

  "relations": {    
    "questions": {
      "type": "embedsMany",
      "model": "FormQuestion",
      "option": {
        "validate": true,
        "autoId": true
      }
    }
  }

如果我按照以下方式发布到http://localhost:3000/api/Forms

{
  "startTime": "",
  "stopTime": "",  
  "formQuestions": [
    {
      "type": 0,
      "label": "a label",
      "content": ["the content"]
    }
  ]
}

它返回:

{
  "startTime": "",
  "stopTime": "", 
  "id": "54ccf7ae6159f1bc0bc6b430",
  "formQuestions": []
}

但是我想将嵌入式模型formQuestion也插入到数据库中,我该怎么做呢?如果有人能帮助我,我将不胜感激。

1 个答案:

答案 0 :(得分:3)

没有人知道,但我找到了自己的方式。

这是因为有一些关于使用嵌入式文档生成自动id的错误。即使我将autoId设置为true,也没用。因此,如果我使用手动ID发布,它将会成功。

就像这样:

{
  "startTime": "",
  "stopTime": "",  
  "formQuestions": [
    {
      "id": 1,
      "type": 0,
      "label": "a label",
      "content": ["the content"]
    }
  ]
}