首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >CASL JS中的“文章”不能执行“删除”

CASL JS中的“文章”不能执行“删除”
EN

Stack Overflow用户
提问于 2022-10-31 16:08:40
回答 1查看 36关注 0票数 0

我正在学习CASL,并尝试删除一篇带有条件的文章,但是得到了这个错误Cannot execute "delete" on "Article"。这是CodeSandBox链路

下面是示例代码:

代码语言:javascript
复制
const { createMongoAbility, ForbiddenError } = require("@casl/ability");

const rules = [
  {
    action: "read",
    subject: "Article"
  },
  {
    inverted: true,
    action: "delete",
    subject: "Article",
    conditions: { published: true },
    reason: "You are not allowed to delete this article"
  }
];

const ability = createMongoAbility(rules);

// this can be pulled from a database
class Article {
  constructor(attrs) {
    Object.assign(this, attrs);
  }
}

const anotherArticle = new Article({
  authorId: 2,
  published: false,
  content: "Lorem Ipsum"
});

try {
  // checking ability before taking some action
  ForbiddenError.from(ability).throwUnlessCan("delete", anotherArticle);
} catch (error) {
  console.log(error.message); // throwing `Cannot execute "delete" on "Article"`
}

请帮帮我。谢谢

EN

回答 1

Stack Overflow用户

发布于 2022-11-02 05:52:14

CASL JS的创建者回答说:

您声明不可能删除已发表的文章,但您从未说过可以删除文章。这就是为什么你会犯错误

因此,这意味着我已经宣布了倒排许可,而且不可能删除文章。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74266082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档