如何在RTK查询中使用Transformesponse更改服务器响应

发布于 2025-02-11 16:22:34 字数 2722 浏览 2 评论 0原文

请我是RTK查询的新手,我做了一个API调用,该调用返回了一系列对象,我想使用 transformResponse 选项将API响应触摸到只有一个对象数组。

这是我的代码


getStocks: builder.query({
    query: credentials => ({
        url: `stocks`,
        body: {credentials},
        transform: response => {
            console.log(response);
            return response;
        },
    }),
   })

,这里是API响应数据


[
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "AAPL"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ],
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "MSFT"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ],
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "BYD"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ],
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "SEC"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ]
]

,这是我要输出数据的方式。

Symbo关闭
AAPL120
BYD130
CSC123
SEC433

Please I am new to RTK query, I made an API call that returns an array of arrays of objects, I want to use the transformResponse option to flatten the API response to just one array of objects.

Here is my code


getStocks: builder.query({
    query: credentials => ({
        url: `stocks`,
        body: {credentials},
        transform: response => {
            console.log(response);
            return response;
        },
    }),
   })

Here is the API response data


[
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "AAPL"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ],
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "MSFT"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ],
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "BYD"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ],
    [
        {
            1. Information: "Monthly Adjusted Prices and Volumes"
            2. Symbol: "SEC"
            3. Last Refreshed: "2022-06-28 16:00:01"
            4. Time Zone: "US/Eastern"
        }, 
        {
            1. open: "101.0000"
            2. high: "118.0000"
            3. low: "91.0600"
            4. close: "102.8100"
            5. adjusted close: "0.7848"
            6. volume: "84091200"
            7. dividend amount: "0.0000"
        }
    ]
]

Here is how I want to output the data.

SymboClose
AAPL120
BYD130
CSC123
SEC433

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深陷 2025-02-18 16:22:34

我能够在 stackoverflow上找到解决方案。理解我自己的解决方案。

我创建了一个自定义basequeryWithChange函数(全球)为所有查询定义了一个转换响应,我用自定义basequerywithchange basequerywithchange

basequery 函数转换API响应

export const changeResponse = async (list) => {
    const dataArray = list.data
    console.log(dataArray) // 10 elements
    
    const finalArray = await dataArray.filter((each) => each.length !== 1) // 6 elements

    const normalizeData = (data) => {
        return data.flatMap((d) => {
            return { ...d[0], ...d[1] };
        });
    };

    const latestList = normalizeData(finalArray)

    console.log(normalizeData(finalArray)); // 6 elements

    return latestList
}

basequery


import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
// import function to transform API response
import { changeResponse } from "../../constants/changeResponse";

const baseQuery = fetchBaseQuery({
    baseUrl: "http://localhost:3500"
})

const baseQueryWithChange = async (args, api, extraOptions) => {
    let result = await baseQuery(args, api, extraOptions);
    if (result.data) {
        result.data = changeResponse(result.data)
        console.log(result.data)
    }
    return result
}

export const apiSlice = createApi({
    baseQuery: baseQueryWithChange,
    endpoints: builder => ({})
})

I was able to find a solution on stackoverflow where I got the understanding to fabricate my own solution.

I created a custom baseQueryWithChange function to (globally) define a transformResponse for all queries and I wrap baseQuery with the custom baseQueryWithChange

function to transform API response

export const changeResponse = async (list) => {
    const dataArray = list.data
    console.log(dataArray) // 10 elements
    
    const finalArray = await dataArray.filter((each) => each.length !== 1) // 6 elements

    const normalizeData = (data) => {
        return data.flatMap((d) => {
            return { ...d[0], ...d[1] };
        });
    };

    const latestList = normalizeData(finalArray)

    console.log(normalizeData(finalArray)); // 6 elements

    return latestList
}

baseQuery


import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
// import function to transform API response
import { changeResponse } from "../../constants/changeResponse";

const baseQuery = fetchBaseQuery({
    baseUrl: "http://localhost:3500"
})

const baseQueryWithChange = async (args, api, extraOptions) => {
    let result = await baseQuery(args, api, extraOptions);
    if (result.data) {
        result.data = changeResponse(result.data)
        console.log(result.data)
    }
    return result
}

export const apiSlice = createApi({
    baseQuery: baseQueryWithChange,
    endpoints: builder => ({})
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文