Quantcast
Channel: Response from Fetch() is undefined - Stack Overflow
Viewing all articles
Browse latest Browse all 2

Response from Fetch() is undefined

$
0
0

There are two files: RegistrationScreen.js and api.js.

Inside api.js I'm running fetch() function:

export const fetchAllUsers = () => {    fetch(`http://${localIP}:${port}/api/userData`)        .then(res => res.json())         /* .then(json => console.warn('JSON: '+ json.userData[0].username))        .catch(e => console.warn('ERROR: '+ e)) */}

If comment back the second .then() I'm able to see username from response.

I'm importing this function into RegistrationScreen.js:

import { fetchAllUsers } from '../../constants/api';export default class RegistrationScreen extends Component{    static defaultProps = {        fetchAllUsers    }    onCreateButtonPress = async () => {        ...        let usernameValidation = await this.checkUsernameUniqueness();        ...    }    checkUsernameUniqueness = async () => {        const data = await this.props.fetchAllUsers();        console.warn('user = '+ data)    }    ...}

And as an outcome I get in the console user = undefined.Why I can see the data inside api.js but not in RegistrationScreen.js?

UPDATE_1

If do console.warn(this.props.fetchAllUsers) inside RegistrationScreen.js I see the function code so the function is visible.


Viewing all articles
Browse latest Browse all 2

Latest Images

Trending Articles





Latest Images

<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>
<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596344.js" async> </script>