Reputation: 329
loggingIn() is my onClick listener.
loggingIn(){
fetch("http://sampleurl.com", {
method: "POST",
headers: {
'Accept': 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: this.state.username,
password: this.state.password,
})
})
.then((response) => response.json())
.then((responseData) => {
console.log(responseData);
if (responseData.response === 1) {
alert("Success");
} else {
alert("Username or password is incorrect");
}
})
.done();
}
Does anybody know why it gives me an error?. When I use this code to react native it works just fine but when I integrate it in reactjs it gives me this error
`TypeError: fetch(...).then(...).then(...).done is not a function`
Upvotes: 1
Views: 11399
Reputation: 31
that problem arises due to missing of the catch block. Mine was resolved after adding the catch block.
fetch(URL, config)
.then(response => response.json())
.then((data) => {})
.catch((error) => {
console.log(error);
});
Upvotes: 3
Reputation: 51
fetch() returns a Promise, on which you can call then() to get the response and catch() to get any errors that were thrown.
Replace .done()
with something like .catch((error) => console.error(error))
.
See here for details: Networking in React Native
Upvotes: 5