You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Strapi多对多关联中通过关联用户过滤项目REST API报错

解决Strapi REST API过滤关联用户项目时的"Invalid key users"错误

使用Strapi的entityService可以正常查询关联指定用户的项目:

const projects = await strapi.entityService.findMany('api::project.project', {
  filters: { users: { id: { $eq: userId } } },
  populate: ['tenant', 'users'],
});

但发送以下REST API请求时:

/api/projects?populate[0]=user&populate[1]=tenant&filters[users][id][$eq]=35

返回400验证错误:

details: {key: "users", path: "users", source: "query", param: "filters"}
message: "Invalid key users"
name: "ValidationError"
status: 400


相关模型Schema

User-Permission Schema

{
  "kind": "collectionType",
  "collectionName": "up_users",
  "info": {
    "name": "user",
    "description": "",
    "singularName": "user",
    "pluralName": "users",
    "displayName": "User"
  },
  "options": {
    "draftAndPublish": false
  },
  "pluginOptions": {},
  "attributes": {
    "username": {
      "type": "string",
      "minLength": 3,
      "unique": true,
      "configurable": false,
      "required": true
    },
    "email": {
      "type": "email",
      "minLength": 6,
      "configurable": false,
      "required": true
    },
    "provider": {
      "type": "string",
      "configurable": false
    },
    "password": {
      "type": "password",
      "minLength": 6,
      "configurable": false,
      "private": true,
      "searchable": false
    },
    "resetPasswordToken": {
      "type": "string",
      "configurable": false,
      "private": true,
      "searchable": false
    },
    "confirmationToken": {
      "type": "string",
      "configurable": false,
      "private": true,
      "searchable": false
    },
    "confirmed": {
      "type": "boolean",
      "default": false,
      "configurable": false
    },
    "blocked": {
      "type": "boolean",
      "default": false,
      "configurable": false
    },
    "role": {
      "type": "relation",
      "relation": "manyToOne",
      "target": "plugin::users-permissions.role",
      "inversedBy": "users",
      "configurable": false
    },
    "mobileNo": {
      "type": "biginteger",
      "unique": true
    },
    "tenant": {
      "type": "relation",
      "relation": "manyToOne",
      "target": "api::tenant.tenant",
      "inversedBy": "users"
    },
    "projects": {
      "type": "relation",
      "relation": "manyToMany",
      "target": "api::project.project",
      "mappedBy": "users"
    }
  }
}

Project Schema

{
  "kind": "collectionType",
  "collectionName": "projects",
  "info": {
    "singularName": "project",
    "pluralName": "projects",
    "displayName": "Project"
  },
  "options": {
    "draftAndPublish": false
  },
  "pluginOptions": {},
  "attributes": {
    "name": {
      "type": "string",
      "unique": true
    },
    "tenant": {
      "type": "relation",
      "relation": "manyToOne",
      "target": "api::tenant.tenant",
      "inversedBy": "projects"
    },
    "description": {
      "type": "text"
    },
    "users": {
      "type": "relation",
      "relation": "manyToMany",
      "target": "plugin::users-permissions.user",
      "inversedBy": "projects"
    }
  }
}

解决方法

1. 修正REST请求参数

请求中的populate[0]=user是错误的,模型中关联字段名为users(复数),正确的请求URL应为:

/api/projects?populate[0]=users&populate[1]=tenant&filters[users][id][$eq]=35

2. 开启Project模型中users字段的可过滤权限

Strapi的REST API默认会验证过滤字段是否允许,entityService不受此限制,需手动开启:

方法A:后台界面配置

  • 进入Strapi后台,打开Content-Types Builder
  • 编辑Project模型,切换到Advanced Settings标签
  • 在Filters区域,勾选users字段
  • 保存后重启Strapi服务

方法B:手动修改Schema文件

在api/project/content-types/project/schema.json中,修改users字段配置并添加REST插件权限:

{
  // ... 保留原有内容
  "pluginOptions": {
    "rest": {
      "config": {
        "filters": {
          "users": true
        },
        "populate": {
          "users": true,
          "tenant": true
        }
      }
    }
  },
  "attributes": {
    // ... 保留原有属性
    "users": {
      "type": "relation",
      "relation": "manyToMany",
      "target": "plugin::users-permissions.user",
      "inversedBy": "projects",
      "filterable": true
    }
  }
}

修改完成后重启Strapi服务。

3. 清理缓存(可选)

如果修改后仍报错,可删除Strapi的缓存目录(.strapi/cache),再重启服务。


内容的提问来源于stack exchange,提问作者Karthick J

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 03:58:10