1

I'm currently working with React Stream Chat and I am attempting to perform a query on the custom data within my channel objects. Each channel object contains a custom data object named "channel_detail" which is structured as follows:

channel_detail: {
    postID: '1234',
    offers: [{
        id: '2345',
        amount: 12
    }],
    chanelType: 'Offer',
    offerMadeBy: '564545454',
}

I aim to fetch all channels that contain a specific "postID" within this "channel_detail" object. To do so, I'm using the following query:

const sort = { last_message_at: -1 };
let filter = { 'data.channel_detail.postID': { '&eq': postID } };

streamClient.queryChannels(filter, sort, {});

Unfortunately, this query is not returning the expected results. I am not receiving any errors, but the channels with the specified 'postID' are not being retrieved.

Has anyone encountered a similar issue or can provide guidance on how to properly structure a query in React Stream Chat to access custom data within a channel? Any advice or assistance would be greatly appreciated.

Tanuj Gupta
  • 286
  • 5
  • 20

1 Answers1

0

it's important to note that GetStream does not support filtering based on nested fields in custom data. So, if you have a custom field structure like:

custom_field: {
  a: {
    b: 'custom_value'
  }
}

You won't be able to filter directly using the nested field like:

const filters = { 'custom_field.a.b': 'custom_value' };

This won't work because the Stream API queryChannels method can only understand and filter based on the first layer of custom data.

You technically can filter on the first-level field, but it probably won't provide the desired results:

const filters = { 'custom_field': { /* value or object you want to match */ } };

In this case, since custom_field is an object, the filter might not behave as expected.

A recommended approach is to keep the custom fields "flat" for easier filtering, or if you must use nested objects, store a stringified version and parse it on the client side for further operations.

Himani
  • 24
  • 3
  • "I can't believe I overlooked this in their documentation! I'll give this solution a try right away. Thank you for your helpful response!" – Tanuj Gupta Jun 26 '23 at 07:24
  • Exact duplicate of this banned ChatGPT answer](https://stackoverflow.com/a/76554443). – tchrist Jul 01 '23 at 00:28