javascript - How to send array as parameter for RTK Query? - Stack Overflow

I want to send array as parameters like this:v1product?main_category[]=3&main_category[]=4 my par

I want to send array as parameters like this:

v1/product?main_category[]=3&main_category[]=4 

my params :

params.main_category = [1,2,3]

my product slice:

export const productsSlice = apiSlice.injectEndpoints({
    endpoints: (builder) => ({
        getProducts: builder.query<
            any,
            {
                title: string;
                ename: string;
                status: string;
                site_product_id: number;
                site_product_meta_id: number;
                per_page: number;
                page: number;
                parent_id: [];
                main_category: [];
                stock_category: [];
                side_category: [];
            }
        >({
            query: (arg) => {
                const {
                    title: ename,
                    status,
                    site_product_id,
                    site_product_meta_id,
                    per_page,
                    page,
                    parent_id,
                    main_category,
                    stock_category,
                    side_category,
                } = arg;
                return {
                    url: "/product",
                    params: {
                        ename,
                        status,
                        site_product_id,
                        site_product_meta_id,
                        per_page,
                        page,
                        parent_id,
                        main_category,
                        stock_category,
                        side_category,
                    },
                };
            },
        }),
        getProduct: builder.query({
            query: (id) => `/product/${id}`,
        }),
    }),
});

but it send like this:

/v1/product?main_category=3%2C4

I want to send array as parameters like this:

v1/product?main_category[]=3&main_category[]=4 

my params :

params.main_category = [1,2,3]

my product slice:

export const productsSlice = apiSlice.injectEndpoints({
    endpoints: (builder) => ({
        getProducts: builder.query<
            any,
            {
                title: string;
                ename: string;
                status: string;
                site_product_id: number;
                site_product_meta_id: number;
                per_page: number;
                page: number;
                parent_id: [];
                main_category: [];
                stock_category: [];
                side_category: [];
            }
        >({
            query: (arg) => {
                const {
                    title: ename,
                    status,
                    site_product_id,
                    site_product_meta_id,
                    per_page,
                    page,
                    parent_id,
                    main_category,
                    stock_category,
                    side_category,
                } = arg;
                return {
                    url: "/product",
                    params: {
                        ename,
                        status,
                        site_product_id,
                        site_product_meta_id,
                        per_page,
                        page,
                        parent_id,
                        main_category,
                        stock_category,
                        side_category,
                    },
                };
            },
        }),
        getProduct: builder.query({
            query: (id) => `/product/${id}`,
        }),
    }),
});

but it send like this:

/v1/product?main_category=3%2C4
Share Improve this question asked May 22, 2022 at 6:06 S.M_EmamianS.M_Emamian 17.4k40 gold badges154 silver badges274 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 6

Per default, RTK Query just uses URLSearchParms for parameter serialization, with the result you see there. But you can override this behaviour by spcifying a paramsSerializer function in fetchBaseQuery. That will allow you to write your own serialization logic or use a library like query-string to do that for you.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745493309a4630082.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信