2

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!

5
  • 5
    your login function doesn't return a promise, that's why you are getting the error. Commented Jul 19, 2021 at 12:38
  • Yep you should use promises, then you can achieve what you described above Commented Jul 19, 2021 at 12:38
  • Do you want const login = async (token) => { and await AsyncStorage.setItem('token', token, (error) => {? According to reactnative.dev/docs/asyncstorage#setitem AsyncStorage.setItem returns a promise. Commented Jul 19, 2021 at 12:39
  • yes how to use promises in login() function Commented Jul 19, 2021 at 12:41
  • Here's the documentation on Promise and that will give you an idea on how to use it in your case: developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… Commented Jul 19, 2021 at 12:47

1 Answer 1

3

You can use async/await to handle the promise returned from AsyncStorage.setItem and return a promise containing token:

const login = async (token) => {
  setLoading(true)

  await 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
}

It seems like you don't even modify token. It doesn't make sense to return it. You can use responseJson.token instead:

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(() => {
      setToken(responseJson.token)

      getMoviesFromApiAsync()
    })
}

and

const login = async (token) => {
  setLoading(true)

  await AsyncStorage.setItem('token', token, (error) => {
    if (token == 'wrong') {
      setIsLoggedIn(false)
      setLoading(false)
      ToastAndroid.show(
        'error',
        ToastAndroid.SHORT
      )
    } else if (!error) {
      setIsLoggedIn(true)
      setLoading(false)
    }
  })
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.