imyzf
imyzf

Reputation: 86

How to post model together with embedded model using Loopback.js?

Here is the form model schema:

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

relations of form.json:

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

If I post to http://localhost:3000/api/Forms as follow,

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

it returns:

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

But I want the embedded model formQuestion also to be inserted into the database, how can I do it? I would be grateful if anyone could help me.

Upvotes: 4

Views: 1667

Answers (1)

imyzf
imyzf

Reputation: 86

Nobody knows, but I find the way myself.

It is because there is some bug about auto id generating with embedded document. It is useless even I set autoId to true. So if I post with manual id, it will success.

Just like this:

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

Upvotes: 3

Related Questions