How do you post an object to Strapi collection that should have a relational data

Viewed 478

I'm using the Strapi CMS for a project and have managed to make a POST request to a "poll" collection in Strapi like this:

fetch("http://localhost:1337/api/polls", {
            method: "POST",
            mode: "cors",
            headers: {
                "Content-type": "application/json; charset=UTF-8",
                "Authorization": "Bearer " + user.jwt
            },
            body: JSON.stringify({
                data: {
                    title: title,
                    description: description,
                    author: 3,
                    questions: questions,
                    pollEndsAt: pollEndsAt,
                    tags: [1, 2]
                }
            })
        }).then(r => r.json()).then(d => {
            console.log("d", d);
        }).catch(err => console.log(err));

and it does manage to find the Tags from the Tag collection, but not the Author which is related to the User collection. Instead, it just saves the object without the author property having a value.

If it's any help, this is what the schema in the poll/content-types/poll map looks like.

{
  "kind": "collectionType",
  "collectionName": "polls",
  "info": {
    "singularName": "poll",
    "pluralName": "polls",
    "displayName": "Poll",
    "description": ""
  },
  "options": {
    "draftAndPublish": true
  },
  "pluginOptions": {},
  "attributes": {
    "title": {
      "type": "string",
      "required": true
    },
    "description": {
      "type": "text",
      "required": true
    },
    "questions": {
      "type": "json",
      "required": true
    },
    "pollEndsAt": {
      "type": "date"
    },
    "author": {
      "type": "relation",
      "relation": "manyToOne",
      "target": "plugin::users-permissions.user",
      "inversedBy": "polls"
    },
    "answered_polls": {
      "type": "relation",
      "relation": "oneToMany",
      "target": "api::answered-poll.answered-poll",
      "mappedBy": "poll"
    },
    "tags": {
      "type": "relation",
      "relation": "manyToMany",
      "target": "api::tag.tag",
      "inversedBy": "polls"
    }
  }
}
0 Answers
Related