首页 > 解决方案 > 将数据从 api 传递到变量 Node.js

问题描述

我从互联网上获得了一个关于 NBA 球队的 api,碰巧我无法将这些数据保存在使用状态中。api已连接,我可以访问它的数据,但我无法将此数据传递给我称为“setEquipa”的变量我的目标是将“团队”中的内容传递给该flalist,从而列出所有NBA球队

代码:

export default function App() {

  const [equipas,setEquipa] = useState([]) 


  useEffect(() => {
      var axio = require("axios").default;
      axio.request(options).then(function (response){
        console.log(response.data)
        axios.request(setEquipa(response.equipas))
      });
 /*var axios = require("axios").default;
  
  axios.request(options).then(function (response){
    console.log(response.data);
  }).catch(function (error) {
    console.error(error);
  });
*/
 })
  const renderItem = ({item}) => {
    return (
      <View style={styles.box}>
        <Text style={styles.teams}>{(item.full_name)}</Text>
      </View>
    )

  }


  return (
    <View>
       <FlatList
          data={equipas}
          renderItem={renderItem}
          keyExtractor={(item) => uuid()}
        >
        </FlatList> 
    </View>
  );
}

接口:

Object {
  "data": Array [
    Object {
      "abbreviation": "ATL",
      "city": "Atlanta",
      "conference": "East",
      "division": "Southeast",
      "full_name": "Atlanta Hawks",
      "id": 1,
      "name": "Hawks",
    },
    Object {
      "abbreviation": "BOS",
      "city": "Boston",
      "conference": "East",
      "division": "Atlantic",
      "full_name": "Boston Celtics",
      "id": 2,
      "name": "Celtics",
    },
    Object {
      "abbreviation": "BKN",
      "city": "Brooklyn",
      "conference": "East",
      "division": "Atlantic",
      "full_name": "Brooklyn Nets",
      "id": 3,
      "name": "Nets",
    },
    Object {
      "abbreviation": "CHA",
      "city": "Charlotte",
      "conference": "East",
      "division": "Southeast",
      "full_name": "Charlotte Hornets",
      "id": 4,
      "name": "Hornets",
    },

标签: node.jsapi

解决方案


推荐阅读