How to update relation field via POST

System Information
  • Strapi Version 4:

I have a project using Next JS 13

This is my create post function

 const handleSubmit = async (event: { preventDefault: () => void; }) => {
    event.preventDefault();
    const response = await fetch(epURL, {
        method: 'POST',
        headers: {
          Authorization: `Bearer ${jwt}`,
          'Content-Type': 'application/json',
        },
        body: JSON.stringify({
          data: {
            title,
            description,
            Owner:{
              data:{
                id:user!.id
              }
            }
          }
        })
      });
      const data = await response.json();
      console.log(data);
    };

Here’s the sample json that needs to be populated

{

    "data": {

        "id": 26,

        "attributes": {

            "title": "test",

            "description": "1",

            "body": null,

            "contact": null,

            "address": null,

            "createdAt": "2023-02-12T09:07:19.631Z",

            "updatedAt": "2023-02-12T09:19:09.462Z",

            "publishedAt": "2023-02-12T09:07:19.629Z",

            "slug": null,

            "Owner": {

                "data": {

                    "id": 13,

                    "attributes": {

                        "username": "testuser",

                        "email": "test@test.com",

                        "provider": "local",

                        "confirmed": true,

                        "blocked": false,

                        "createdAt": "2023-02-10T02:40:24.875Z",

                        "updatedAt": "2023-02-12T04:48:26.560Z",

                        "firstname": "test",

                        "lastname": "test"

                    }

                }

            }

        }

    },

    "meta": {}

}

The problem is that I cannot assign the Owner data which is a relation field from user
The post request seems to be successful but it does not reflect.

1 Like

Nevermind,

resolved it with

Owner:{
id: user!.id
}
1 Like

Thank you so much ! I had been stuck on this for days !

1 Like