Strapi v4 - Querying users-permission plugin model: Error: Model role not found
Strapi v4 - Querying users-permission plugin model: Error: Model role not found
我正在尝试将我的应用程序从 Strapi v3 升级到 v4,并且查询插件模型似乎有重大变化。
这行代码(适用于 v3):
const publicRole = await strapi
.query("role", "users-permissions")
.findOne({ type: "public" });
产生以下错误:“错误:找不到模型角色”
这是完整代码:
async function setPublicPermissions(strapi, newPermissions) {
// Find the ID of the public role
const publicRole = await strapi
.query("role", "users-permissions")
.findOne({ type: "public" }); <---- produces Error: Model role not found
// List all available permissions
const publicPermissions = await strapi
.query("permission", "users-permissions")
.find({ type: "application", role: publicRole.id });
// Update permission to match new config
const controllersToUpdate = Object.keys(newPermissions);
console.log({ controllersToUpdate });
const updatePromises = publicPermissions
.filter((permission) => {
// Only update permissions included in newConfig
if (!controllersToUpdate.includes(permission.controller)) {
return false;
}
if (!newPermissions[permission.controller].includes(permission.action)) {
return false;
}
return true;
})
.map((permission) => {
// Enable the selected permissions
return strapi
.query("permission", "users-permissions")
.update({ id: permission.id }, { enabled: true });
});
await Promise.all(updatePromises);
}
版本 4 中是否有查询插件模型的新方法来实现此功能?
我找到了答案。在 Strapi v4 的插件中查询模型如下所示:
const publicRole = await strapi
.query("plugin::users-permissions.role")
.findOne({
where: {
type: "public",
},
});
我正在尝试将我的应用程序从 Strapi v3 升级到 v4,并且查询插件模型似乎有重大变化。
这行代码(适用于 v3):
const publicRole = await strapi
.query("role", "users-permissions")
.findOne({ type: "public" });
产生以下错误:“错误:找不到模型角色”
这是完整代码:
async function setPublicPermissions(strapi, newPermissions) {
// Find the ID of the public role
const publicRole = await strapi
.query("role", "users-permissions")
.findOne({ type: "public" }); <---- produces Error: Model role not found
// List all available permissions
const publicPermissions = await strapi
.query("permission", "users-permissions")
.find({ type: "application", role: publicRole.id });
// Update permission to match new config
const controllersToUpdate = Object.keys(newPermissions);
console.log({ controllersToUpdate });
const updatePromises = publicPermissions
.filter((permission) => {
// Only update permissions included in newConfig
if (!controllersToUpdate.includes(permission.controller)) {
return false;
}
if (!newPermissions[permission.controller].includes(permission.action)) {
return false;
}
return true;
})
.map((permission) => {
// Enable the selected permissions
return strapi
.query("permission", "users-permissions")
.update({ id: permission.id }, { enabled: true });
});
await Promise.all(updatePromises);
}
版本 4 中是否有查询插件模型的新方法来实现此功能?
我找到了答案。在 Strapi v4 的插件中查询模型如下所示:
const publicRole = await strapi
.query("plugin::users-permissions.role")
.findOne({
where: {
type: "public",
},
});