Reputation: 321
im trying to use async function to consume my api, but it simply doesent work, it didnt even console log anything, here is my code:
import React, { Component } from 'react';
import { View, Text, StyleSheet, Dimensions } from 'react-native';
import { ScrollView } from 'react-native-gesture-handler';
import {NavigationContainer} from '@react-navigation/native'
import { SafeAreaView } from 'react-navigation';
import Profile from './Profile'
import api from '../services/api';
//import Home from './Mainpage'
const {heigth, width} = Dimensions.get('window')
here is just styles
styles = StyleSheet.create({
Container:{
flex:1,
justifyContent:"flex-end",
alignItems: "flex-end",
},
PlaceContainer:{
width: '100%',
maxHeight:200,
},
button:{
width:"10px",
height:"10px"
},
title:{
fontWeight:'bold',
fontSize: 20
},
place:{
width:width -40 ,
padding:20,
maxHeight:200,
marginHorizontal: 20,
marginVertical: 8,
backgroundColor: '#0ff'
},
separator:{
flex: 1,
height: StyleSheet.hairlineWidth,
backgroundColor: '#8E8E8E',
}
})
here I created a class to make everything
export default class Dermatologistas extends Component{
state ={
MessageError: null,
users: []
}
that is not working, it isnt even console logging
getUserList = async () => {
try {
const response = await api.get('/auth/list');
const { users } = response.data;
this.setState({ users });
} catch (response) {
this.setState({ errorMessage: response.data.error });
}
};
just rendered to test
render(){
const users = this.state.users
in this console.log it return Array []
console.log(users)
return(
<View>
{this.state.users.map(user => (
<View key={user._id} style={{marginTop: 15}}>
<Text>{user.title}</Text>
<Text>{user.speciality}</Text>
</View>
))}
</View>
)
}
}
Upvotes: 0
Views: 50
Reputation: 260
Where's the call to getUserList? Also, to your get method you should include the promise method then() which followed by a catch() method.
To summarize, your call should look like that:
fetch(url)
.then(function() {
})
.catch(function() {
});
Upvotes: 1