question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

$elemMatch caches its state and never re-evaluates to false!

See original GitHub issue

Describe the bug It seems that using $elemMatch and ability.can() from the useAbility hook causes the first true returned by ability.can() to be applied to all subsequent can() calls when iterating over an array of elements.

To Reproduce Steps to reproduce the behavior:

import { AbilityBuilder, Ability, AbilityClass } from '@casl/ability';
import { TeamFragment } from '../../generated/graphql';
import subjectTypeFromGraphql from './subjectTypeFromGraphql';

type Abilities = ['manage', 'Team' | TeamFragment];

type AppAbility = Ability<Abilities>;
export const AppAbility = Ability as AbilityClass<AppAbility>;

export default function defineAbilityFor() {
  const { can, build } = new AbilityBuilder(AppAbility);

  can('manage', 'Team', {
    groupMembers: {
      $elemMatch: {
        'account.id': '8461e09d-7a54-4649-9427-28e71dff9e73'
        'roles.roleId': 'admin',
      },
    },
  });

  return build({ detectSubjectType: subjectTypeFromGraphql });
}
import { detectSubjectType } from '@casl/ability';

export default (subject: any) => {
  if (subject?.__typename) {
    return subject.__typename;
  }

  return detectSubjectType(subject);
};

  1. How do you check abilities

I have an array of two Teams. The first I should not be able to manage, and the second I should. On first render, this is shown correctly. When the component re-renders, it’s incorrectly showing that both Teams are now manageable.

If I reverse the order of the array, both are shown as manageable on first and additional renders. I believe the true being returned by the ability.can('manage', team) for the first item is being cached or memoized somewhere for the second, even though they are separate objects.

I’ve hardcoded the account id for simplicity at the moment.

Expected behavior I expect each call of ability.can('manage', team) in a loop to be specific the team object that was passed in.

CASL Version

@casl/ability - 4.1.6 @casl/react - 2.1.1

Environment: Chrome 85, Typescript 3.9.

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:10 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
martdavidsoncommented, Sep 22, 2020

It’s good! Thank you very much 😃

1reaction
stalniycommented, Sep 22, 2020

@martdavidson could you please check whether all good? The sift issue was closed

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to get a result from $elemMatch (example: True/False)
I tried declaring my find value to a variable and made an if-else statement using None (my app is in python, None basically...
Read more >
Mongodb querying find subdodument and query
Like computed properties, a getter's result is cached based on its dependencies, and will only re-evaluate when some of its dependencies ...
Read more >
Ocean Ambiguity ring 392 elem Compliance to Magnetic fascism
Students Propose New Designs for Their School's Journalism Lab - Center for Architecture ... elemMatch caches its state and never re-evaluates to false!...
Read more >
MongoDB: The Definitive Guide, Second Edition - TINET
MongoDB users can start with Chapter 1and be up and running in no time. Experienced ... It uses as much of RAM as...
Read more >
5.4. Multithreading Problems
Since it has the cache line in shared state, it can read it without and coherence ... To fix a false sharing problem...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found