7

I'm trying to send JSON data using react-native, axios and Expo, but when I press "Send" on my application, I get this warning:

Possible unhandled promise rejection, Error: Network Error

My API is correctly receiving the notification (JSON) when I try to send it via POSTman, but when I try to send it using axios, I get the above warning message, so maybe react is not sending the data correctly.

export default class NotificationsInput extends React.Component {

state = {
    token: '',
    title: '',
    body: ''
}

handleToken = event => {
    this.setState({ token: event.target.value })
}

handleTitle = event => {
    this.setState({ title: event.target.value })
}

handleBody = event => {
    this.setState({ body: event.target.value })
}

handleSubmit = event => {
    event.preventDefault();

    let notification = JSON.stringify({
        token: this.state.token,
        title: this.state.title,
        body: this.state.body
    })

    let headers = {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    }
    axios.post(`http://127.0.0.1:8000/send_push_message/`, notification, headers)
        .then(res => {
            console.log(res);
            console.log(res.data)
        })
        .then(error => console.log(error));

}

render() {
    return (
        <View>
            <TextInput onChange={this.handleToken}
                style={{ height: 25, width: 200, borderColor: 'black', borderWidth: 1 }}
            />
            <TextInput onChange={this.handleTitle}
                style={{ height: 40, width: 200, borderColor: 'black', borderWidth: 1 }}
            />
            <TextInput onChange={this.handleBody}
                style={{ height: 40, width: 200, borderColor: 'black', borderWidth: 1 }}
            />
            <Button onPress={this.handleSubmit} title="Send">Send</Button>
        </View>
    )
}

}

Edit :

I added the catch() function, but the error now is only Network Error in the console.

handleSubmit = event => {
    event.preventDefault();

    let notification = JSON.stringify({
        token: this.state.token,
        title: this.state.title,
        body: this.state.body
    })

    let headers = {
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }

    }
    axios.post(`http://127.0.0.1:8000/send_push_message/`, notification, headers)
    .then(res => {
        console.log(res);
        console.log(res.data)
    })
    .catch(error => console.log(error));

}
Pavindu
  • 2,684
  • 6
  • 44
  • 77
Biel Borba
  • 165
  • 1
  • 2
  • 5

5 Answers5

4

I can see you have chained two, .then which is not correct. You need a catch block to catch network errors. Take a look below

  axios.post(`http://127.0.0.1:8000/send_push_message/`, notification, headers)
    .then(res => {
        console.log(res);
        console.log(res.data)
    })
    .catch(error => console.log(error));
Shubham Gupta
  • 2,596
  • 1
  • 8
  • 19
  • @Rik i meant in his piece of code. he was trying to catch err in .then – Shubham Gupta Sep 25 '18 at 04:30
  • 1
    i tried adding a catch, but this only make the error pop in my console – Biel Borba Sep 25 '18 at 04:31
  • @BielBorba that is correct which means your call is getting rejected from backend. I think it looking for some data in payload which it is not finding and failing. https://medium.com/@dtinth/making-unhandled-promise-rejections-crash-the-node-js-process-ffc27cfcc9dd – Shubham Gupta Sep 25 '18 at 04:35
1

Use catch instead of then for error handling.

.catch(error => console.log(error));
Rik
  • 467
  • 4
  • 14
1

Solved. Changed:

return axios.get('http://localhost:3000/hello')

to:

return axios.get('http://10.0.0.2:3000/hello')

and it works. In Android emulator, the localhost refers its device.

https://github.com/facebook/react-native/issues/10404

DaveL17
  • 1,673
  • 7
  • 24
  • 38
janierrl
  • 11
  • 1
0

If you have cors enabled in your backend stack,

then try replacing 127.0.0.1 with 10.0.2.2

Hope this helps.

Mr Coolman
  • 29
  • 4
0

Check again your api_url.

It should include 'http' or 'https'

Tugay Tuna
  • 11
  • 3