Error: StreamChat error code 70: QueryChannels failed with error: "N channels match your query but cannot be returned because you don't have access to them. Did you forget to include {members: $in: [ ... ]}?"
(following up from https://github.com/GetStream/stream-chat-react/issues/1822)
Here is a simple example where the connected user can change over time: CodeSandbox (make sure to update the constants.ts to your own)
When I click "User 2" and then "User 1" again, I get this error message:
Error: StreamChat error code 70: QueryChannels failed with error: "2 channels match your query but cannot be returned because you don't have access to them. Did you forget to include {members: $in: ["..."]}?"
As you can see in App.jsx, { members: { $in: [ ... ] } } is provided.
Does it get out of sync between the two users somehow?
What's the recommended way to avoid this error?
@moroshko the reason for the error logged is that the first HTTP request for /channels endpoint after the user switch is performed with the old (other user's) token. Then the request is repeated with the correct (2nd user's) token.
This could be considered an edge case, because the user is not usually switched in the modeled manner, but rather has to log out and in. Do you have any use case for this scenario?
I would suggest you to have a flag that controls, whether the user switch is taking place within your useConnectUser hook.
Please, share your thoughts.
Thanks, @MartinCupela for taking a look!
You're right, currently, in our app, the user has to log out before logging in as someone else. However, we might add a feature for testing purposes where there is a dropdown of available users and you can easily switch between them.
As far as the useConnectUser hook is concerned, I believe it should be agnostic of how users are switched in the app, and implement its own mechanism to protect against such calls with a wrong token.
I would suggest you to have a flag that controls, whether the user switch is taking place within your useConnectUser hook.
Do you mind updating the CodeSandbox above to demonstrate how it could work?
@moroshko would the following hook help you?
https://github.com/GetStream/website-react-examples/blob/master/social-messenger-ts/src/hooks/useConnectUser.ts
@MartinCupela I tried this hook (see here), but hit the error described in this issue.
I'm running into the same issue
okay so my issue was I was using memoized filters when switching between users so not using useMemo resolved it.
@MartinCupela Just following up to see if there were any new developments on this since last year and whether the error above can be avoided these days?
The issue was due to how effects work in React. You can assure that you are querying channels for a user by taking the user id from the client.user.id. Example sandbox here.