stream-chat-react icon indicating copy to clipboard operation
stream-chat-react copied to clipboard

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: [ ... ]}?"

Open moroshko opened this issue 3 years ago • 7 comments

(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 avatar Oct 25 '22 23:10 moroshko

@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.

MartinCupela avatar Oct 26 '22 09:10 MartinCupela

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 avatar Nov 01 '22 05:11 moroshko

@moroshko would the following hook help you?

https://github.com/GetStream/website-react-examples/blob/master/social-messenger-ts/src/hooks/useConnectUser.ts

MartinCupela avatar Nov 10 '22 12:11 MartinCupela

@MartinCupela I tried this hook (see here), but hit the error described in this issue.

moroshko avatar Nov 10 '22 22:11 moroshko

I'm running into the same issue

ah0y avatar Jun 28 '23 03:06 ah0y

okay so my issue was I was using memoized filters when switching between users so not using useMemo resolved it.

ah0y avatar Jun 28 '23 03:06 ah0y

@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?

moroshko avatar Aug 14 '23 01:08 moroshko

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.

MartinCupela avatar May 27 '24 18:05 MartinCupela