axios 调用从第一个 api 获取响应并传递到第二个 api
Posted
技术标签:
【中文标题】axios 调用从第一个 api 获取响应并传递到第二个 api【英文标题】:axios call getting response from first api and passing into second api 【发布时间】:2019-11-26 15:58:18 【问题描述】: 我有两个不同的 api。 从第一个 api 响应中,我需要将 lowId 传递给第二个 api 请求以获取 SportSpecialty。 我有两个不同的操作调用不同的 api。 但问题是我不确定如何将值从第一个 api 传递到第二个 api。调试了代码,这是我获取 id 的地方
.then(response =>
response.data.helpFar.map(entity =>
console.log(
'fetchhighRelationshipByBalanceId response--->',
entity
);
);
研究并找到了这个链接,但仍然没有帮助我。
axios get - multiple api calls with response from first api to subsequent calls
Axios: chaining multiple API requests
在下面提供我的代码 sn-p 和 json 响应。第一个 api 调用
export function fetchhighRelationshipByBalanceId(
lowId,
isHistory,
callBack
)
console.log('fetchhighRelationshipByBalanceId--->');
return dispatch =>
this._source = axios.CancelToken.source();
return axios
.get(
`http://localhost:1000/star/dull/?lowId=$lowId&isHistory=$isHistory`,
cancelToken: this._source.token,
)
.then(response =>
response.data.helpFar.map(entity =>
console.log(
'fetchhighRelationshipByBalanceId response--->',
entity
);
);
callBack(response);
)
.catch(error =>
if (axios.isCancel(error))
console.log('-- Request canceled', error.message);
else
console.log('-- Error', error.message);
);
;
第二次 API 调用
export function fetchlowRelationshipByBalanceId(
lowId,
isHistory,
callBack
)
console.log('fetchlowRelationshipByBalanceId--->');
return dispatch =>
this._source = axios.CancelToken.source();
return axios
.get(
`http://localhost:1000/star/bright/?lowId=$lowId&isHistory=$isHistory`,
cancelToken: this._source.token,
)
.then(response =>
callBack(response);
)
.catch(error =>
if (axios.isCancel(error))
console.log('-- Request canceled', error.message);
else
console.log('-- Error', error.message);
);
;
第一个 api 响应
http://talk.com/jujuj/766723237623/dulls
"helpFar" : [
"lowId" : 78787878,
"lowName" : "fdjdfndfdfknkdfs",
"lowStartDate" : "2015-12-10",
"lowEndDate" : "2016-01-07",
"finger" : [
"sport2" : 5656565656232323323232332233775,
"sport3" : "fdjdfndfdfknkdfs",
"sport4" : "sdfsddddddddddd",
"sport5" : "898989898989123123123123123123123123123123123123123123123",
"sport1" : "reererer",
"fdkdlmkdflkdf211212" : "2010-07-01"
]
,
"lowId" : 7878787878787878787877252,
"lowName" : " =er8989erer8eru8989uer89er89gu89",
"lowStartDate" : "2015-12-10",
"lowEndDate" : "2016-01-07",
"finger" : [
"sport2" : 2378782356237823787878127884818,
"sport3" : "jkjkjksdjkdsjksdjk MEDICAL sdfsddddddddddd OF ",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "90er90er9090re90er90",
"sport1" : "reerer",
"fdkdlmkdflkdf211212" : "237878235623782378787812787-10-30"
,
"sport2" : 5656565656232323323232332233294,
"sport3" : "jkjkjksdjkdsjksdjk fdfd",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "90er90er9090re90er90",
"sport1" : "reerer",
"fdkdlmkdflkdf211212" : "2010-06-30"
,
"sport2" : 5656565656232323323232332233779,
"sport3" : " =er8989erer8eru8989uer89er89gu89",
"sport4" : "sdfsddddddddddd",
"sport5" : "90er90er9090re90er90",
"sport1" : "ererer",
"fdkdlmkdflkdf211212" : "2010-07-01"
,
"sport2" : 20139872,
"sport3" : "sdsdsdsd",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "90er90er9090re90er90",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "2018-10-27"
]
,
"lowId" : 7878787878787878787885126,
"lowName" : "erauioioioioioioio =er8989erer8eru8989uer89er89gu89",
"lowStartDate" : "2010-07-01",
"lowEndDate" : "9999-12-31",
"finger" : [
"sport2" : 5656565656232323323232332233290,
"sport3" : "jkjkjksdjkdsjksdjk erauioioioioioioiofdfd",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "898989898989",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "2010-06-30"
,
"sport2" : 5656565656232323323232332233778,
"sport3" : "erauioioioioioioio =er8989erer8eru8989uer89er89gu89",
"sport4" : "sdfsddddddddddd",
"sport5" : "898989898989",
"sport1" : "rereer",
"fdkdlmkdflkdf211212" : "2010-07-01"
]
,
"lowId" : 7878787878787878787885115,
"lowName" : "eruiaeruiruieeruiuier",
"lowStartDate" : "2010-06-30",
"lowEndDate" : "9999-12-31",
"finger" : [
"sport2" : 5656565656232323323232332233279,
"sport3" : "jkjkjksdjkdsjksdjk LAKE SHOREfdfd",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "oiioioioio",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "2010-06-30"
]
,
"lowId" : 7878787878787878787800157,
"lowName" : "jkjkjksdjkdsjksdjk df dfdfsvvf",
"lowStartDate" : "2017-12-04",
"lowEndDate" : "9999-12-31",
"finger" : [
"sport2" : 2378782356237823787878127800170,
"sport3" : "jkjkjksdjkdsjksdjk df dfdfsvvf",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "7676767676",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127800791,
"sport3" : "jkjkjksdjkdsjksdjk vccvxcx",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "7676767676",
"sport1" : "errerereer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127801096,
"sport3" : "jkjkjksdjkdsjksdjk dfadfadfdffdd",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "7676767676",
"sport1" : "errereer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 20128386,
"sport3" : "erreerre",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "7676767676",
"sport1" : "erreerer",
"fdkdlmkdflkdf211212" : "1947-12-01"
,
"sport2" : 2013787878787878787878784,
"sport3" : "jkjkjksdjkdsjksdjk df dfdfsvvf",
"sport4" : "sdfsddddddddddd",
"sport5" : "7676767676",
"sport1" : "ererererer",
"fdkdlmkdflkdf211212" : "2017-12-04"
]
,
"lowId" : 7878787878787878787800159,
"lowName" : "jkjkjksdjkdsjksdjk dfdffffffffffffffff",
"lowStartDate" : "237878235623782378787812786-07-27",
"lowEndDate" : "9999-12-31",
"finger" : [
"sport2" : 2378782356237823787878127800172,
"sport3" : "jkjkjksdjkdsjksdjk AND UNIVERSITY OF FLORIDA HEALTH CARE NTWK",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "r9we9rew9090q90",
"sport1" : "reererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127800477,
"sport3" : "GENERAL SURGERY AT AYERS",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "r9we9rew9090q90",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127800558,
"sport3" : "jkjkjksdjkdsjksdjk HOMECARE",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "r9we9rew9090q90",
"sport1" : "ererererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127801313,
"sport3" : "ALACHUA GEN HOSPTIAL SKILLED NURSING UNIT",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "r9we9rew9090q90",
"sport1" : "erreerer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127801746,
"sport3" : "jkjkjksdjkdsjksdjk REHAB HOSPITAL",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "r9we9rew9090q90",
"sport1" : "ererererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127802174,
"sport3" : "jkjkjksdjkdsjksdjk MEDICAL sdfsddddddddddd MAGNOLIA PARKE",
"sport4" : "sdfsddddddddddd",
"sport5" : "r9we9rew9090q90",
"sport1" : "ererreerer",
"fdkdlmkdflkdf211212" : "1999-01-01"
,
"sport2" : 2378782356237823787878127802227,
"sport3" : "jkjkjksdjkdsjksdjk dfdffffffffffffffff",
"sport4" : "sdfsddddddddddd",
"sport5" : "r9we9rew9090q90",
"sport1" : "rerereerererre",
"fdkdlmkdflkdf211212" : "1999-01-01"
,
"sport2" : 2378782356237823787878127807706,
"sport3" : "jkjkjksdjkdsjksdjk HOSPITAL",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "ererererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127807707,
"sport3" : "ererererer INC",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127807708,
"sport3" : "jkjkjksdjkdsjksdjkfdfdf dfffffffffff",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "ererreer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127807709,
"sport3" : "jkjkjksdjkdsjksdjk HOME CARE",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "erreerer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127807710,
"sport3" : "jkjkjksdjkdsjksdjk dfdfdfdffd",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "erreerre",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127807711,
"sport3" : "jkjkjksdjkdsjksdjk HOSPITAL (DME)",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "R6093",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127807712,
"sport3" : "jkjkjksdjkdsjksdjk dsdsdsdsd",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "237878235623782378787812784-01-01"
,
"sport2" : 2378782356237823787878127877859,
"sport3" : "FCPA/jkjkjksdjkdsjksdjk dfsdfdfdfdfdf",
"sport4" : "idfosaooooooooooooooo",
"sport5" : "r9we9rew9090q90",
"sport1" : "reerreer",
"fdkdlmkdflkdf211212" : "237878235623782378787812786-07-27"
,
"sport2" : 5656565656232323323232332238954,
"sport3" : "jkjkjksdjkdsjksdjk VISTA , jkjkjksdjkdsjksdjk AT VISTA",
"sport4" : "sdsdsdsdsdsdsdsdsdsdsdsda",
"sport5" : "r9we9rew9090q90",
"sport1" : "erreerer",
"fdkdlmkdflkdf211212" : "2011-10-21"
,
"sport2" : 20116360,
"sport3" : "jkjkjksdjkdsjksdjk dfdffffffffffffffff",
"sport4" : "sdfsddddddddddd",
"sport5" : "r9we9rew9090q90",
"sport1" : "erererer",
"fdkdlmkdflkdf211212" : "2014-11-19"
]
],
"highId" : "766723237623",
"highName" : "jkjkjksdjkdsjksdjk"
第二个 api 响应
http://talk.com/jujuj/78787878/brights
"lowdfdfdfdfs" : [
"sport2" : 5656565656232323323232332233775,
"sport3" : "fdjdfndfdfknkdfs",
"sport4" : "sdfsddddddddddd",
"sport5" : "898989898989123123123123123123123123123123123123123123123",
"sport1" : "000WD",
"sportRole" : "Specialist",
"sportSpecialty" : "Multi-Specialty Physician sdfsddddddddddd-19323787823562378237878781278000X",
"fdkdlmkdflkdf211212" : "2010-07-01",
"paymentPreference" :
"dfmklId" : 78787878,
"dfmklName" : "fdjdfndfdfknkdfs",
"dfmklTaxId" : "898989898989123123123123123123123123123123123123123123123",
"dfmklStartDate" : "2010-07-13",
"dfmklEndDate" : "9999-12-31",
"dfmkl1" : "000WD",
"dfmklAddress" :
"addressLine1Text" : "PO BOX 11414",
"addressLine3Text" : "Belfast, ME 04915",
"addressLine4Text" : "Waldo"
],
"highId" : "766723237623",
"highName" : "jkjkjksdjkdsjksdjk"
【问题讨论】:
【参考方案1】:你的代码比你的解释复杂。
以此代码开头
function fetchStar(type, lowId, isHistory)
let token = axios.CancelToken.source();
let body = cancelToken: token ;
let url = `http://localhost:1000/star/$type/?lowId=$lowId&isHistory=$isHistory`;
return axios.get(url, body);
你不需要这两个蹩脚的功能。
这样使用
fetchStar(type:'dull',lowId:'???', isHistory:false)
.then( response =>
let whatEverYouWant = null; //find value you need from response.data
return fetchStar(type:'bright', lowId:whatEverYouWant, isHistory:false);
)
.catch( console.error );
【讨论】:
感谢您的回复,您能否更新我的代码 sn-p 太难理解了 :( 嘿,我的第一个 api 调用是在不同的方法fetchhighRelationshipByBalanceId
和第二个 api 调用是在不同的方法 fetchlowRelationshipByBalanceId
但在你的情况下你使用单一方法 fetchStar
你能解释更多关于它,所以困惑:(
两个 fetch 函数之间的唯一区别是类型参数。【参考方案2】:
链接你的承诺。这是一个示例,您应该能够根据自己的需要进行调整。
const firstPromise = () => Promise.resolve(foo: 'bar');
const secondPromise = bar => Promise.resolve(bar + 'baz');
firstPromise()
.then(data => data.foo)
.then(secondPromise)
.then(data =>
console.log(data); // 'barbaz'
)
【讨论】:
感谢您的回复,您能否更新我的代码 sn-p 太难理解了 :( 嘿,使用 promise 是好方法或 await 是好方法。我提到了这个链接medium.com/@bluepnume/… 在这里开始开发原型,你能告诉我我的方向是否正确吗codesandbox.io/s/redux-async-actions-xjdo7以上是关于axios 调用从第一个 api 获取响应并传递到第二个 api的主要内容,如果未能解决你的问题,请参考以下文章
ReactJS-在http请求的axios方法中将JWT令牌作为授权传递
在 Heroku 部署中响应对 Express API 的 axios 调用
React Hook useEffect :使用 axios 和 async await .api 获取数据,调用连续相同的 api
Lua - 如何传递一个 API 调用获得的字符串来调用另一个 API 调用