如何在RTK查询中使用Transformesponse更改服务器响应
请我是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 | 关闭 |
---|---|
AAPL | 120 |
BYD | 130 |
CSC | 123 |
SEC | 433 |
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.
Symbo | Close |
---|---|
AAPL | 120 |
BYD | 130 |
CSC | 123 |
SEC | 433 |
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够在 stackoverflow上找到解决方案。理解我自己的解决方案。
我创建了一个自定义
basequeryWithChange
函数(全球)为所有查询定义了一个转换响应,我用自定义basequerywithchange
basequerywithchangebasequery 函数转换API响应
basequery
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 wrapbaseQuery
with the custombaseQueryWithChange
function to transform API response
baseQuery