Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

Getting Type error is not a constructor in Realm with react native typescript

I'm building out a react native app with typescript and wanted to test out realm. i create my getRealm.ts file with the following code:

import Realm from "realm";

let app;

// Returns the shared instance of the Realm app.
export function getRealmApp() {
  if (app === undefined) {
    const appId = process.env.MONGO_REALM_API!; // Set Realm app ID here.
    const appConfig = {
      id: appId,
      timeout: 10000,
      app: {
        name: "default",
        version: "0",
      },
    };
    app = new Realm.App(appConfig);
  }
  return app;
}

and followed with the file where i hoped to use it in:

import React, { useState } from "react";
import { Button, View } from "react-native";
import { Provider } from "react-redux";
import Textbox from "./components/textbox";
import { getRealmApp } from "./realm/getRealmApp";
import { store } from "./redux";
import Realm from "realm";

const app = getRealmApp();

const MainEntry = () => {
  const [email, setEmail] = useState("[email protected]");
  const [password, setPassword] = useState("testing123");

  const longinHandler = async () => {
    const creds = Realm.Credentials.emailPassword(email, password);
    const newUser = await app.logIn(creds);

    console.log(newUser);
  };

  return (
    <Provider store={store}>
      <View>
        <Textbox placeholder="Email" />
        <Textbox placeholder="password" />
      </View>

      <Button title={"Login"} onPress={longinHandler} />
    </Provider>
  );
};

export default MainEntry;

I saved the file and wanted to test the login function just see what type of data is being sent back. The moment i saved the file, the application couldn't run and returned :

 WARN     Require cycle: index.js -> App.tsx -> src/index.tsx -> src/realm/getRealmApp.ts -> index.js

Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle.
[Sun Jan 24 2021 16:27:44.683]  WARN     Require cycle: index.js -> App.tsx -> src/index.tsx -> index.js

Require cycles are allowed, but can result in uninitialized values. Consider refactoring to remove the need for a cycle.
[Sun Jan 24 2021 16:27:44.684]  ERROR    TypeError: undefined is not a constructor (evaluating 'new _realm.default.App(appConfig)')
[Sun Jan 24 2021 16:27:44.684]  ERROR    Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)
[Sun Jan 24 2021 16:27:44.685]  ERROR    Invariant Violation: Module AppRegistry is not a registered callable module (calling runApplication)

Picture of the errorPlease help

question from:https://stackoverflow.com/questions/65871802/getting-type-error-is-not-a-constructor-in-realm-with-react-native-typescript

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...