Reactjs 从mlab获取react native中的数据
我正在尝试学习使用Reactjs 从mlab获取react native中的数据,reactjs,react-native,backend,mlab,Reactjs,React Native,Backend,Mlab,我正在尝试学习使用apikey从服务器获取数据。我用这个创建了一些文档。 在文档中,我无法在代码中列出项目 从“React”导入React;导入{FlatList,ActivityIndicator, 文本,视图}来自“react native” 导出默认类FetchExample扩展React.Component{ 建造师(道具){ 超级(道具); this.state={isLoading:true} componentDidMount(){ 返回获取('https://api.mlab.
apikey
从服务器获取数据。我用这个创建了一些文档。
在文档中,我无法在代码中列出项目
从“React”导入React;导入{FlatList,ActivityIndicator,
文本,视图}来自“react native”
导出默认类FetchExample扩展React.Component{
建造师(道具){
超级(道具);
this.state={isLoading:true}
componentDidMount(){
返回获取('https://api.mlab.com/api/1/databases/ibrhmklnc/collections/kitap?apiKey=xRw1H6NNZZnyeZArU5L8oUoVcsFb6tpf')
.then((response)=>response.json())
.然后((responseJson)=>{
这是我的国家({
孤岛加载:false,
数据来源:responseJson.movies,
},函数(){
});
})
.catch((错误)=>{
控制台错误(error);
}); }
render(){
if(此.state.isLoading){
返回(
)
}
返回(
{item.title},{item.releaseYear}
keyExtractor={({id},索引)=>id}
/>
); } }
以下是API的完整响应
[
{
_id: { '$oid': '5be7260efb6fc072d46759b5' },
movies: [
{ id: '1', title: 'Star Wars', releaseYear: '1977' },
{ id: '2', title: 'Back to the Future', releaseYear: '1985' },
{ id: '3', title: 'The Matrix', releaseYear: '1999' },
{ id: '4', title: 'Inception', releaseYear: '2010' },
{ id: '5', title: 'Interstellar', releaseYear: '2014' }
]
}
]
如您所见,API响应是一个数组。因此,为了获取电影列表,您应该将数据源设置为:
dataSource: responseJson[0].movies,
下面是API的完整响应
[
{
_id: { '$oid': '5be7260efb6fc072d46759b5' },
movies: [
{ id: '1', title: 'Star Wars', releaseYear: '1977' },
{ id: '2', title: 'Back to the Future', releaseYear: '1985' },
{ id: '3', title: 'The Matrix', releaseYear: '1999' },
{ id: '4', title: 'Inception', releaseYear: '2010' },
{ id: '5', title: 'Interstellar', releaseYear: '2014' }
]
}
]
如您所见,API响应是一个数组。因此,为了获取电影列表,您应该将数据源设置为:
dataSource: responseJson[0].movies,