jwarris91
jwarris91

Reputation: 952

Handling API Calls in React Native?

I am new to react native and trying to interact with an API helper I've made in one of my screen files, I'm sure this is a syntax error but I'm also interested if the architecture of it is correct as I come form iOS development.

The function with the error:

  handleLoginPress = () => {
    ApiHelper.loginDriver(this.state.email, this.state.password)
      .then((result) => {
        console.log(result);
    }).catch((error) => {
        console.log("error is " + error);
    });
  };

Error is:

Property 'then' does not exist on type 'void'

The function its calling:

static loginDriver(username: string, password: string) {
    fetch('http://localhost:8080/drivers/login', {
         method: 'POST',
    headers: {
        Accept: 'application/json',
        'Content-Type': 'application/json',
    },
    body: JSON.stringify({
        uername: username,
        password: password,
    }),
    });
}

My thought is there needs to be some specification as to the return type on the apis login function?

Upvotes: 0

Views: 406

Answers (1)

Junius L
Junius L

Reputation: 16152

return is missing from your loginDriver function, add a return statement to return the data.

static loginDriver(username: string, password: string) {
  return fetch('http://localhost:8080/drivers/login', {
    method: 'POST',
    headers: {
      Accept: 'application/json',
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({
      uername: username,
      password: password
    })
  });
}

Upvotes: 1

Related Questions