realm / realm-js

Realm is a mobile database: an alternative to SQLite & key-value stores
https://realm.io
Apache License 2.0
5.8k stars 577 forks source link

Failed to execute 'fetch' on 'Window': member signal is not of type AbortSignal. #3005

Closed cameroncruz closed 4 years ago

cameroncruz commented 4 years ago

Hi, I'm running into issues integrating basic authentication using Realm with Electron. I believe this issue is related to https://github.com/Azure/azure-sdk-for-js/issues/5143. I noticed that in realm-js, lib/NetworkTransport.js line 46 is where global fetch is being retrieved. While avoiding global fetch is the solution azure-sdk-for-js decided to pursue, is there a short-term solution I can apply to get unblocked? Thanks.

Goals

I want to enable email/password Login based on https://docs.mongodb.com/realm/tutorial/react-native/ for my Electron app.

Expected Results

Calling app.logIn(creds) on a Realm.App to return a valid user.

Actual Results

I receive the following error: Failed to execute 'fetch' on 'Window': member signal is not of type AbortSignal.

Steps to Reproduce

Following this tutorial https://docs.mongodb.com/realm/tutorial/react-native/ with Electron.

Code Sample

app = new Realm.App(appConfig);
const creds = Realm.Credentials.emailPassword(email, password);
const newUser = await app.logIn(creds);

Version of Realm and Tooling

cameroncruz commented 4 years ago

I built realm@v10.0.0-beta.6 from source with the following change to NetworkTransport.js lines 43-47:

if (!DefaultNetworkTransport.fetch) {
    // Try to get it from the global
    if (false) {
        DefaultNetworkTransport.fetch = fetch.bind(window);
    }

which seems to do the trick. Is there any motivation to continue trying to get global fetch?

kraenhansen commented 4 years ago

Are you running this code from the main or a renderer process? If you are available, it would be great to get a repository with a minimal app reproducing this error.

kraenhansen commented 4 years ago

Okay - I managed to reproduce the error in the renderer process, using the following project structure:

package.json

{
  "name": "realm-js-electron-test",
  "version": "0.1.0",
  "private": true,
  "main": "index.js",
  "scripts": {
    "test": "electron index.js"
  },
  "devDependencies": {
    "electron": "^8.1.1"
  },
  "dependencies": {
    "realm": "^10.0.0-beta.7"
  }
}

.npmrc

target=8.1.1
runtime=electron
disturl=https://atom.io/download/electron

index.js

const { app, BrowserWindow } = require("electron");
const Realm = require("realm");

/*
async function useRealm() {
    const app = new Realm.App("realmwebtestapp-ybnna");
    const credentials = Realm.Credentials.anonymous();
    const user = await app.logIn(credentials);
    console.log(user);
}

app.on("ready", () => {
    console.log("App is ready - lets use Realm");
    useRealm().then(() => {
        console.log("All done ...");
    }, err => {
        console.error(err);
        process.exit(1);
    });
});
*/

app.on("ready", () => {
    const window = new BrowserWindow({ webPreferences: {  nodeIntegration: true } });
    window.loadFile("index.html");
});

index.html

<!DOCTYPE html>
<html>
  <head>
    <meta charset="UTF-8">
    <title>Testing Realm JS</title>
    <meta http-equiv="Content-Security-Policy" content="script-src 'self' 'unsafe-inline';" />
  </head>
  <body>
    <h1>Testing Realm JS</h1>
    <script>
        const Realm = require("realm");

        async function useRealm() {
            const app = new Realm.App("realmwebtestapp-ybnna");
            const credentials = Realm.Credentials.anonymous();
            const user = await app.logIn(credentials);
            console.log(user);
        }

        useRealm().then(() => {
            console.log("All done ...");
        }, err => {
            console.error(err);
            process.exit(1);
        });
    </script>
  </body>
</html>
kraenhansen commented 4 years ago

The PR has been merged, you can expect a fix for this in the upcoming v10 beta release.