【发布时间】:2022-01-03 04:21:55
【问题描述】:
我正在尝试通过 api 获取数据。
api客户端:
export interface apiRequest {
locations: LocationOptions[];
}
interface FOLocations {
locationId: number;
}
interface LocationResult {
location: FOLocations[];
cityName: string;
}
export const locationCheck = async (
apiKey: string,
payload: apiRequest
): Promise<LocationResult[]> => {
let response = await axios.post<LocationResult[]>(
`api...`,
payload
);
return response.data;
运行代码:
const locationPayload : apiRequest = {
locations:[{ cityName: "London"},{cityName: "New York"},{cityName: "Paris"}],
};
const locationResponse = await locationCheck(apiKey,locationPayload);
const [locationResponseRes] = locationResponse;
console.log(locationResponseRes);
实际输出:
{
"location": {
"locationId": 1,
"cityName: "London",
}
}
预期输出(我在 Postman 中得到的):
[
{
"location": {
"locationId": 1,
"cityName: "London",
}
},
{
"location": {
"locationId": 2,
"cityName": "New York",
},
},
]
因此,如您所见,并非所有数据都显示在数组中。也许我需要通过 entity[] 来做?
【问题讨论】:
-
const [locationResponseRes] = locationResponse;这使用解构并从响应数组中获取第一个对象,只需使用 ` const locationResponseRes = locationResponse; `
标签: javascript arrays typescript postman