Search code examples
node.jsapinext.jspostmanstrapi

POST relation in strapi


I have a strapi project and A single users have relation a single profile. For example if we using postman to get a single user it have response

[
{
    "id": 45,
    "username": "test",
    "email": "test@gmail.com",
    "provider": "local",
    "confirmed": true,
    "blocked": false,
    "createdAt": "2022-07-18T08:50:43.642Z",
    "updatedAt": "2022-07-18T08:50:43.642Z",
    "profile": null
}

] As you can see a user have a profile but have a null value. So when I tried to post a profile in strapi api http://localhost:1337/api/profiles?populate=* with body of json like this

{
"data" : {
"name": "fgh",
"address": "jksdjkdjs",
"phone": "345345",
"email" : "test@gmail.com" /// I tried to add this but the response is 500 internal server error
}}

It worked and status 200 ok but it didn't get the relation of the email from user

{
"data": {
    "id": 12,
    "attributes": {
        "name": "fghdff",
        "address": "jksdjkdjs",
        "phone": 34534235,
        "createdAt": "2022-08-05T13:48:29.548Z",
        "updatedAt": "2022-08-05T13:48:29.548Z",
        "publishedAt": "2022-08-05T13:48:29.547Z",
        "email": {
            "data": null
        }
    }
},
"meta": {}}

Any Idea how to post a relation in strapi? I tried to read docs or strapi forum but no one counter this problem


Solution

  • For someone facing how to POST relation in strapi we can use the ID of the User. For example to post and connect in my question is

    {   
    "data" : {
        "name": "dayee",
        "address": "jksdjkdjs",
        "phone": "34534235",
        "email" : 77 // Post relation using ID
    }}