我有两个API呼叫第二是取决于第一个API呼叫响应
我想首先从地理定位 API 获取坐标,然后借助从第一个 API 调用获取的坐标获取天气状况。
const { data: cords } = useQuery("cords", () => {
navigator.geolocation.getCurrentPosition((position) => {
});
});
const { isLoading, isError, error, data } = useQuery(["weather"], () => {
return axios.get(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=myapikey`
);
});
I want to get coordinates first from the geolocation API and then fetch weather conditions with the help of coordinates that I got from the first API call.
const { data: cords } = useQuery("cords", () => {
navigator.geolocation.getCurrentPosition((position) => {
});
});
const { isLoading, isError, error, data } = useQuery(["weather"], () => {
return axios.get(
`https://api.openweathermap.org/data/2.5/weather?lat=${lat}&lon=${lon}&appid=myapikey`
);
});
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
对于第二个查询,您必须使用
启用
用于依赖的操作员查询。您可以执行
启用:!! CORDS
,因此在有cords
时将执行。编辑:
在您的情况下,这是这样的:
这样,只有在
cords
具有值时,第二个查询才会触发,并且每次都会发射cords
值,它已更新已更新。For the second query you have to use the
enabled
operator for dependent queries.You can do
enabled: !!cords
so it gets executed when there’scords
.Edit:
In your case it'd be like this:
This way the second query will only fire when the
cords
have a value and it will fire every timecords
value it's updated.