javascript - React Native issue : [Unhandled promise rejection: TypeError: f is not a function. (In 'f()', &

I am trying to make a login system using states and fetch so that the user can receive his valid token

I am trying to make a login system using states and fetch so that the user can receive his valid token and then put it in storage with Async Storage. I am still in the phase of munication with the API (expressjs). Everything works for the moment when I enter the email and the password I do receive the token in json but I have a warning type error which tells me:

[Unhandled promise rejection: TypeError: f is not a function. (In 'f()', 'f' is undefined)]

My code is very simple for the moment (I have absolutely no idea where it es from):

const onSubmit = () => {
    setLoading(true)
    fetch('http://192.168.1.36:3000/api/users/login', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        email: email,
        password: password
      })
    })
    .then((response) => response.json())
    .then((res) => console.log(res))
    .catch((err) => console.log(err))
    .finally(setLoading(false));
  }

I am trying to make a login system using states and fetch so that the user can receive his valid token and then put it in storage with Async Storage. I am still in the phase of munication with the API (expressjs). Everything works for the moment when I enter the email and the password I do receive the token in json but I have a warning type error which tells me:

[Unhandled promise rejection: TypeError: f is not a function. (In 'f()', 'f' is undefined)]

My code is very simple for the moment (I have absolutely no idea where it es from):

const onSubmit = () => {
    setLoading(true)
    fetch('http://192.168.1.36:3000/api/users/login', {
      method: 'POST',
      headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json'
      },
      body: JSON.stringify({
        email: email,
        password: password
      })
    })
    .then((response) => response.json())
    .then((res) => console.log(res))
    .catch((err) => console.log(err))
    .finally(setLoading(false));
  }

I don't know if the syntax I use with fetch is "good" and "optimize" for a login system, but I hope I was clear enough in my explanation.

Tanks

Share Improve this question edited Jan 17, 2021 at 21:39 e-frank 74911 silver badges22 bronze badges asked Jan 17, 2021 at 21:15 user15026148user15026148
Add a ment  | 

1 Answer 1

Reset to default 7

I think this has to do with your finally clause:

.finally(setLoading(false));

The finally function must be passed a function itself. Two ways to do this:

.finally(() => setLoading(false));
.finally(setLoading.bind(undefined, false));

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745385958a4625440.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信