0

Here's my Todo model structure:

Todo
- title (Text)
- description (Text)
- completed (Boolean)
- users_permissions_user (Relation with User from users-permissions)



User
- username (Text)
- email (Email)
- provider (Text)
- password (Password)
- resetPasswordToken (Text)
- confirmationToken (Text)
- confirmed (Boolean)
- blocked (Boolean)
- role (Relation with Role from users-permissions)
- todos (Relation with Todo)

I've tried to create a Todo with the following request body:


{
  "data": {
    "title": "Your Todo Title",
    "description": "Your Todo Description",
    "completed": false,
    "users_permissions_user": {
      "connect": [3] // Replace with the actual user ID
    }
  }
}

But I'm getting this error:

{
    "data": null,
    "error": {
        "status": 400,
        "name": "ValidationError",
        "message": "Invalid key users_permissions_user",
        "details": {
            "key": "users_permissions_user",
            "path": "users_permissions_user",
            "source": "body"
        }
    }
}

I've also tried variations like:

{
  "data": {
    "title": "Your Todo Title",
    "description": "Your Todo Description",
    "completed": false,
    "users_permissions_user": 3
  }
}

And:

{
  "data": {
    "title": "Your Todo Title",
    "description": "Your Todo Description",
    "completed": false,
    "users_permissions_user": {
      "id": 3
    }
  }
}

But I'm still getting the same error.

How can I properly associate a user with a Todo item during creation in Strapi v5? Am I missing something in my model configuration or API request?

3 Answers 3

0

Check the permission for Users-permissions in API Tokens there should be allowed to find users.

Sign up to request clarification or add additional context in comments.

Comments

0

For Strapi v5, the correct format for creating a Todo with a user association is:

{
  "data": {
    "title": "Your Todo Title",
    "description": "Your Todo Description",
    "completed": false,
    "user": 3
  }
}

The key difference is that in Strapi v5, you should use user instead of users_permissions_user in the request payload, even though the relation field might be named users_permissions_user in your model.

If you want to modify your Todo collection type to make this more explicit, you can update your schema configuration:

{
  "kind": "collectionType",
  "collectionName": "todos",
  "info": {
    "singularName": "todo",
    "pluralName": "todos",
    "displayName": "Todo"
  },
  "options": {
    "draftAndPublish": false
  },
  "attributes": {
    "title": {
      "type": "string"
    },
    "description": {
      "type": "text"
    },
    "completed": {
      "type": "boolean",
      "default": false
    },
    "user": {
      "type": "relation",
      "relation": "manyToOne",
      "target": "plugin::users-permissions.user",
      "inversedBy": "todos"
    }
  }
}

To create a Todo item with an associated user using the Strapi REST API, you can make a POST request:

curl -X POST http://localhost:1337/api/todos -H "Content-Type: application/json" -d '{"data": {"title": "Your Todo Title", "description": "Your Todo Description", "completed": false, "user": 3}}'

To automatically associate todos with the currently authenticated user in your custom controller, you can modify the create handler:

module.exports = createCoreController('api::todo.todo', ({ strapi }) => ({
  async create(ctx) {
    const user = ctx.state.user;
    const { data } = ctx.request.body;
    
    const newData = {
      ...data,
      user: user.id
    };
    
    const response = await super.create({ ...ctx, request: { body: { data: newData } } });
    return response;
  }
}));

Thank you.

Comments

0

Did you try enable User permission stuff? Go to Users & Permissions plugin > User-permissions >

and enable these chechboxes.

Note: Be sure these configuration is ok for your application structure and logic. Maybe you don't wanna give user details. Check, happy coding.

enter image description here Strapi User Permissions

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.