realm-js: failed to execute 'send' on 'xmlhttprequest'

hi, while trying to work with remote debugging (#562 and #571) i upgraded to rn 0.32 and now i’m seeing this when enabling the remote debugging:

Failed to execute 'send' on 'XMLHttpRequest': Failed to load 'http://192.168.1.178:8082/create_session'.

i understand there’s some issues with remote debugging. the ip address is the ip of the android device

About this issue

  • Original URL
  • State: closed
  • Created 8 years ago
  • Reactions: 18
  • Comments: 28 (4 by maintainers)

Most upvoted comments

My guess is as follows. Running on a device and debugging fails because normally adb forward tcp:8082 tcp:8082 takes care of the correct redirection from device to host. Because you are debugging from chrome http://<ip-address>:8082/create_session is an invalid url.

I changed the following lines in node_modules/realm/lib/browser/rpc.js

// The global __debug__ object is provided by Visual Studio Code.
if (global.__debug__) {
    let request = global.__debug__.require('sync-request');
    let response = request('POST', url, {
      body: JSON.stringify(data),
      headers: {
        "Content-Type": "text/plain;charset=UTF-8"
      }
    });

    statusCode = response.statusCode;
    responseText = response.body.toString('utf-8');
} else {
    let body = JSON.stringify(data);
    let request = new XMLHttpRequest();

    request.open('POST', url, false);
    request.send(body);

    statusCode = request.status;
    responseText = request.responseText;
}

to

// The global __debug__ object is provided by Visual Studio Code.
if (global.__debug__) {
    let request = global.__debug__.require('sync-request');
    let response = request('POST', url, {
      body: JSON.stringify(data),
      headers: {
        "Content-Type": "text/plain;charset=UTF-8"
      }
    });

    statusCode = response.statusCode;
    responseText = response.body.toString('utf-8');
} else {
    let body = JSON.stringify(data);
    let request = new XMLHttpRequest();

    // ALWAYS POINT TO LOCALHOST
    if (__DEV__) {
        url = 'http://localhost:8082' + url.substring(url.lastIndexOf('/'));
    }

    request.open('POST', url, false);
    request.send(body);

    statusCode = request.status;
    responseText = request.responseText;
}

And now everything works fine (even when not debugging remotely) I hope this helps

react-native link realm doesn’t fix this issue

We are experiencing this same issue in the iOS simulator. Remote debugging just breaks for us.

Same issue here. Really need debugging on device to proceed with Realm.

Im using React 0.55.4 just installed realm, Trying to debug an iOS build from a Mac Facing the same issue, tried the fix above didn’t work, everything seems to be working fine if I don’t ‘debug remotely’, But I start getting these errors once I do try to debug remotely:

'Access to XMLHttpRequest at 'http://localhost:8081/create_session' from origin 'http://192.168.1.21:8081' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.'

'Must first create RPC session with a valid host'

'Unhandled JS Exception: Must first create RPC session with a valid host'

in my case, debugging of physical device is working, but emulator it is not working

The issue with crashing app at the first launch has found! It seems start from v2.23 there is a BUG in “/src/ios/platform.mm”

Here is missed ! sign in this IF: if ([manager createDirectoryAtPath:docsDir withIntermediateDirectories:YES attributes:nil error:&error]) https://github.com/realm/realm-js/blob/master/src/ios/platform.mm#L78

therefore after deletion of app from device at first launch this IF returned TRUE and the throw std::runtime_error(util::format("Failed to create directory \"%1\": %2", docsDir.UTF8String, error_description(error).UTF8String)); is fired and app crashes.

This bug was created by this commit: https://github.com/realm/realm-js/commit/9c6d26aa591d33cd9161cd40f389da33c340ccda#diff-2c49b41809c315e7530a10428f8a80f5

and I surprised that noone still did not create a ticket about this 😃

in my case on iOS 12.2 (realm 2.26.1 and/or 2.28)

// ALWAYS POINT TO LOCALHOST
if (__DEV__) {
  url = 'http://localhost:8082' + url.substring(url.lastIndexOf('/'));
}

or

...localhost:8083...

do not work.

App crashes on first launch after removing and re-installing app from the phone, on both Debug and Release builds.

Partly fixed by downgrade realm to 2.2.10 BUT it also contain this issue for iOS 12.2: https://github.com/realm/realm-js/issues/2305

So without fork it is not possible to use realm in my project! “Nice” work 😃

Selman555 thank you for answer. it worked for me. Now i can debug android in windows

still have same issue at android device …QQ