【问题标题】:axios swapi api call on embedded url - return value嵌入式url上的axios swapi api调用-返回值
【发布时间】:2022-07-03 21:17:47
【问题描述】:

API 新手,使用 https://swapi.dev/ API。

https://swapi.dev/api/people 上,我正在尝试提取“homeworld”属性。它以 URL 的形式存在。

但我真的希望用行星的名称来代替 URL。

[![例如:[1]][1] [1]:https://i.stack.imgur.com/oKWOb.gif

    const getPeople = async () => {
    try{
        //fetch data from api
        const res = await axios.get('https://swapi.dev/api/people')
        const pl = await axios.get('https://swapi.dev/api/planets/')
        //set People
        setPeople(res.data.results)
        setPlanet(pl.data.results)
        setLoading(true)
        // console.log(res.data.results)
        People.map((item)=>{console.log(item.name)})
        Planet.map((x) => {console.log(x.name)});
        Planet.map((y) => {console.log(y.residents)});
        // console.log(pl.data.results)
    } catch (err) {
        alert(err.message);
    }
}

如何让 Planet item.name 替换 people item.homeland URL 值?

【问题讨论】:

    标签: javascript axios


    【解决方案1】:

    我有同样的问题,使用axios来获取电影,然后在映射中我想为每个movie.characters使用,但似乎有问题。即使我使用未指定的接口/类型,我也会得到链接列表,并且不知道如何处理它们。

    【讨论】:

      猜你喜欢
      • 2021-10-02
      • 2020-03-19
      • 1970-01-01
      • 2021-03-24
      • 2020-03-23
      • 1970-01-01
      • 2019-02-09
      • 2021-12-23
      • 2023-03-21
      相关资源
      最近更新 更多