Javascript Axios 获取条件参数
Javascript Axios get with conditional params
我正在尝试从 API 中获取一些数据。问题是 GET 调用可能采用 none 或某些过滤器。我试过但不确定 how/if 我可以创建一个带有条件过滤器的 URL。
actions: {
InstitutionSearch(value, id){
let fips = id['ParamValues'].find(o=>o.param==='fips')['value'];
let region = id['ParamValues'].find(o=>o.param==='region')['value'];
axios.get('https://educationdata.urban.org/api/v1/college-university/ipeds/directory/2019/',{
params:{
fips: ,
region: region,
offering_highest_level: 3
}
})
};
}
这是一个 vue 应用程序,上面的代码 运行 在 vuex 存储中。传入的 id 是一个对象数组,取自搜索过滤器表单。我遇到的问题是我的查询可能包括 fips 或 region 或 none.
我最初的想法是 put fips 和 region 等于 0,但这不适用于我的 API。我不反对在条件语句中构建查询字符串,但必须有更简单的方法。以下是我正在使用的数据的实际查询 https://educationdata.urban.org/api/v1/college-university/ipeds/directory/2019/?offering_highest_level=3.
在一些惊人的帮助下,我找到了一个简单的解决方案。我创建了一个空对象,然后 运行 对我的参数进行了条件检查,只有在它们符合我的条件时才添加它们。然后我将该对象作为参数传入,一切正常。
let fips = id['ParamValues'].find(o=>o.param==='fips')['value'];
let region = id['ParamValues'].find(o=>o.param==='region')['value'];
//code change
let params = {};
fips.length > 0 ? params['fips'] = fips.join(',') : null;
region != 0 ? params['region'] = region : null;
//code change
axios.get('https://educationdata.urban.org/api/v1/college-university/ipeds/directory/2019/',{
params
}).then(response=>{
console.log(response.data.results);
});
useEffect(() => {
axios
.get(
`http://stream-restaurant-menu-svc.herokuapp.com/item?category=${props.data}`
)
.then((response) => {
console.log("This is to show sub-categoary " + response.data);
setSubcate(response.data);
})
.catch((error) => {
console.log(error);
});
},[props]);
我正在尝试从 API 中获取一些数据。问题是 GET 调用可能采用 none 或某些过滤器。我试过但不确定 how/if 我可以创建一个带有条件过滤器的 URL。
actions: {
InstitutionSearch(value, id){
let fips = id['ParamValues'].find(o=>o.param==='fips')['value'];
let region = id['ParamValues'].find(o=>o.param==='region')['value'];
axios.get('https://educationdata.urban.org/api/v1/college-university/ipeds/directory/2019/',{
params:{
fips: ,
region: region,
offering_highest_level: 3
}
})
};
}
这是一个 vue 应用程序,上面的代码 运行 在 vuex 存储中。传入的 id 是一个对象数组,取自搜索过滤器表单。我遇到的问题是我的查询可能包括 fips 或 region 或 none.
我最初的想法是 put fips 和 region 等于 0,但这不适用于我的 API。我不反对在条件语句中构建查询字符串,但必须有更简单的方法。以下是我正在使用的数据的实际查询 https://educationdata.urban.org/api/v1/college-university/ipeds/directory/2019/?offering_highest_level=3.
在一些惊人的帮助下,我找到了一个简单的解决方案。我创建了一个空对象,然后 运行 对我的参数进行了条件检查,只有在它们符合我的条件时才添加它们。然后我将该对象作为参数传入,一切正常。
let fips = id['ParamValues'].find(o=>o.param==='fips')['value'];
let region = id['ParamValues'].find(o=>o.param==='region')['value'];
//code change
let params = {};
fips.length > 0 ? params['fips'] = fips.join(',') : null;
region != 0 ? params['region'] = region : null;
//code change
axios.get('https://educationdata.urban.org/api/v1/college-university/ipeds/directory/2019/',{
params
}).then(response=>{
console.log(response.data.results);
});
useEffect(() => {
axios
.get(
`http://stream-restaurant-menu-svc.herokuapp.com/item?category=${props.data}`
)
.then((response) => {
console.log("This is to show sub-categoary " + response.data);
setSubcate(response.data);
})
.catch((error) => {
console.log(error);
});
},[props]);