2

I am having some trouble using couchdb in react native. See code below :

const urlcouchdb = 'http://192.168.58.1:5984';

export const login = async (name, password) => {

  const response = await fetch(`${urlcouchdb}/_session`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify({
      name,
      password,
        }),
    }).catch(function(error) {
        console.log("error = " + error);
        return error;
    });
    if (
        response.headers &&
        response.headers.map['set-cookie'] &&
        response.headers.map['set-cookie'][0]
      ) {
        await AsyncStorage.setItem(
          'cookiecouchdb',
          response.headers.map['set-cookie'][0],
        );
      }    
  return response.json();
}

At first, I was using my localhost IP (127.0.0.1), and I was getting this error : TypeError: Network request failed.

After some researches, I've figured out I'd better change it to the IP address of system. I thought my problem was solved, because I was not getting the error anymore, but it turned out that I was still getting the same error, but two minutes (approximatly), after doing the request!

It's very annoying to wait two minutes every single time I try to solve it. Do you have any idea why my request fails?

Just to let you know : The name and password I send to login function are correct. Also, I am testing on my android device, using expo.

souki
  • 1,305
  • 4
  • 23
  • 39
  • If you're on Android, this might be helpful: https://stackoverflow.com/questions/4779963/how-can-i-access-my-localhost-from-my-android-device (I googled "hit local server from Android"). Waiting for 2 minutes after every change is too long indeed. You could try reducing the timeout of the fetch(), or kill the app after 10s if the request hangs and you don't get a response. – Martin Konicek Feb 09 '18 at 17:40

0 Answers0