并非所有数据都显示在数组中
Not all data is displayed in an array
我正在尝试通过 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[] 来完成?
解构赋值语法是一个 JavaScript 表达式,可以将数组中的值或对象中的属性解压缩到不同的变量中。因此,不是从数组中解压第一个对象,而是 return 数组本身。
我添加了一个小示例,以便您更好地理解解构的工作原理:
[a, b] = [10, 20];
console.log(a); // 10
[first, second] = [10, 20, 30, 40, 50];
console.log(second); // 20
[first, second, ...rest] = [10, 20, 30, 40, 50];
console.log(rest); // [30, 40, 50]
底线,return locationResponse
而不是 locationResponseRes
对象(该行可以删除)
我正在尝试通过 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[] 来完成?
解构赋值语法是一个 JavaScript 表达式,可以将数组中的值或对象中的属性解压缩到不同的变量中。因此,不是从数组中解压第一个对象,而是 return 数组本身。
我添加了一个小示例,以便您更好地理解解构的工作原理:
[a, b] = [10, 20];
console.log(a); // 10
[first, second] = [10, 20, 30, 40, 50];
console.log(second); // 20
[first, second, ...rest] = [10, 20, 30, 40, 50];
console.log(rest); // [30, 40, 50]
底线,return locationResponse
而不是 locationResponseRes
对象(该行可以删除)