Javascript fetch方法从上一个api调用返回数据
我正在尝试根据用户选择获取数据。 第一个API调用(getLocationForUser)工作正常并返回正确的值。但是,在用户选择位置之后,我正试图对另一个端点进行另一个API调用(getRoomForUser),但它一直从getLocationForUser cal加载数据Javascript fetch方法从上一个api调用返回数据,javascript,node.js,fetch,Javascript,Node.js,Fetch,我正在尝试根据用户选择获取数据。 第一个API调用(getLocationForUser)工作正常并返回正确的值。但是,在用户选择位置之后,我正试图对另一个端点进行另一个API调用(getRoomForUser),但它一直从getLocationForUser cal加载数据 我试图使用fetch函数,也尝试使用sync请求 我只测试了getRoomForUser API,它返回了预期的数据 switch (userSelection) { case 'puerto rico':
- 我试图使用fetch函数,也尝试使用sync请求
- 我只测试了getRoomForUser API,它返回了预期的数据
switch (userSelection) { case 'puerto rico': await this.getLocationForUser(currentEmployee); break; case 'user set a location': await this.getRoomForUser(context) break; case 'user set a room': await this.setAttendance(context) break; default: } async getLocationForUser(currentEmployee){ let getLocations = "https://sampleGetLocations.com"; fetch( getLocations, { method: 'GET', headers: { Authorization: 'auto dfgasdg' } }) .then(response => response.json() ) .then(json => { data1.push(json.outPutSite[0].value) }); return data1; } async getRoomForUser(currentEmployee){ let getRooms = "https://sampleGetRoomForUser.com"; fetch( getRooms, { method: 'GET', headers: { Authorization: 'auto dfgasdg' } }) .then(response => response.json() ) .then(json => { data2.push(json.outPutSite[0].value) }); return data2; }
每个API调用getLocationForUser和getRoomForUser返回相关数据您是否故意省略了
中断在开关中的“波多黎各”案例
之后的code>语句
?不,对不起,我现在添加它。您的意思是什么@randomSoul@Peter-对不起,这是一行代码。因此,不需要显式的return
。即使在我放置return data2.push(json.outPutSite[0].value)时,我仍在获取GetLocationForUsers的数据。您是否故意省略了中断在开关中的“波多黎各”案例
之后的code>语句
?不,对不起,我现在添加它。您的意思是什么@randomSoul@Peter-对不起,这是一行代码。因此,不需要显式的return
。即使在我放置return data2.push(json.outPutSite[0].value)时,我仍然获取getLocationForUser的数据