node.js - 将数据从 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",
},
解决方案
推荐阅读
- python-3.x - 如何允许用户在 Python Flask 中创建类并链接他们的数据库?
- c++ - 在下面的这种情况下,是否有任何理由从使用 const char* 更改为 string
- mysql - 每天计算累积用户的正确方法是什么?
- ruby-on-rails - ActionMailer::Base:Class 的未定义方法“show_previews”
- javascript - React setState 两次渲染状态
- angular - 如何使用 AngularFire、Firestore 和 Firebase 分页到上一页
- apache-spark - Spark从连接输出聚合多个列(可能是数组)
- android - 视图被裁剪超过 10.000 像素宽度
- java - 现有数据库上的 flyway 迁移导致“列中遇到错误的列类型”
- c# - 按相关性/排名对搜索结果进行排序