strapi / strapi

🚀 Strapi is the leading open-source headless CMS. It’s 100% JavaScript/TypeScript, fully customizable, and developer-first.
https://strapi.io
Other
63.94k stars 8.14k forks source link

Error During User Update: ‘Expected a Valid Number’ for Relation Field #22107

Open Carexo opened 2 weeks ago

Carexo commented 2 weeks ago

Bug report

Required System information

OS: darwin-arm64 Strapi Version: 5.1.0 Node/Yarn Version: npm/10.6.0 node/v22.0.0 darwin arm64 workspaces/false Edition: Community Database: postgres In project: Typescript

Describe the bug

During the update of the saved_attractions field, an error occurs when sending a request with documentId to associate attractions in the user model in Strapi. The error message reads:

Expected a valid Number, got zci5jswm0m1r7hdlg343j50y
Error: Expected a valid Number, got zci5jswm0m1r7hdlg343j50y

The error does not occur when sending the id from the attractions table instead of documentId.

Steps to reproduce the behavior

  1. Go to the Strapi API endpoint /user/me for the user model.
  2. Send a PUT request to update the saved_attractions field with documentId.
  3. Observe the error message that appears.

Expected behavior

The saved_attractions field should be updated without error when using documentId, similarly to when using the id from the attractions table.

Code snippets

Here’s the function used to update the user in the user-permission extension.

  plugin.controllers.user.updateMe = async (ctx) => {
    if (!ctx.state.user || !ctx.state.user.id) {
      return (ctx.response.status = 401);
    }

    await strapi
      .query("plugin::users-permissions.user")
      .update({
        where: { id: ctx.state.user.id },
        data: ctx.request.body,
      })
      .then(() => {
        ctx.response.status = 200;
        ctx.response.body = { message: "User updated successfully" };
      });
  };

Here's the body of the PUT request to /user/me

{
    "saved_attractions": {
        "set": [
            "zci5jswm0m1r7hdlg343j50y"
        ]
    }
}

Additional context

The bug is triggered when the PUT /user/me request, created within the user-permission extension in Strapi, includes the saved_attractions array with documentId. The issue seems to stem from expecting a id instead of the documentId.

Matb85 commented 2 weeks ago

Does not work for me either

chrisd1997 commented 2 weeks ago

Also occurs in the latest release v5.4.1