I've worked with react native and I have a problem...
const SignIn = () => {
screensplash()
fetch('http://api.megamouj.ir/api/v1/logincheck/', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({
username: username,
password: password
})
})
.then((response) => response.json())
.then((responseJson) => {
login(responseJson.token).then(
(token) => {
setToken(token)
getMoviesFromApiAsync()
}
)
})
}
and when program goes on
login(responseJson.token)
I faced this error:
Possible Unhandled Promise Rejection (id: 0): TypeError: Cannot read property 'then' of undefined
and this is my login() function:
const login = (token) => {
setLoading(true)
AsyncStorage.setItem('token', token, (error) => {
if (token == 'wrong') {
setIsLoggedIn(false)
setLoading(false)
ToastAndroid.show(
'error',
ToastAndroid.SHORT
)
} else if (!error) {
setIsLoggedIn(true)
setLoading(false)
}
})
return token }
help me please!
loginfunction doesn't return a promise, that's why you are getting the error.const login = async (token) => {andawait AsyncStorage.setItem('token', token, (error) => {? According to reactnative.dev/docs/asyncstorage#setitemAsyncStorage.setItemreturns a promise.login()functionPromiseand that will give you an idea on how to use it in your case: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…