【问题标题】:Realm error - _constructor must be of type 'function', got (undefined)领域错误 - _constructor 必须是“函数”类型,得到(未定义)
【发布时间】:2018-10-09 08:04:02
【问题描述】:

我正在尝试使用我的 react 本机应用程序在本地设置 Realm DB,但似乎有一个我无法找出原因的错误。我遵循了documentation 和指南here

我的代码。

import Realm from 'realm';

export const ConfigSchema = {
    name: 'Config',
    primaryKey: 'key',
    properties: {
        key: 'string',
        value: 'string'
    }
};

export const databaseOptions = {
    path: 'myappreactnative.realm',
    schema: [ConfigSchema],
    schemaVersion: 0
};

export const insertNewConfig = (newConfig) => new Promise((resolve, reject) => {
    Realm.open(databaseOptions).then(realm => {
        // realm.create('Config', newConfig);
        // resolve(newConfig);
        console.log(realm);
    }).catch((error) => reject(error))
});

我从这里打给insertNewConfig

let config = {
    key: 'instanceUrl',
    value: 'myurl.domain.value'
};

insertNewConfig(config).then((result) => {
    console.log(result);
}).catch((error) => {
    console.log(error);
});

this.props.navigation.navigate('Login', {});

错误出现在Realm.open(databaseOptions) 行。首先我虽然错误是realm.create,但后来意识到原来的行。

错误显示是这样的。

Error: _constructor must be of type 'function', got (undefined)
    at sendRequest (rpc.js:263)
    at Object.createRealm (rpc.js:62)
    at new Realm (index.js:102)
    at Function.open (extensions.js:110)
    at eval (eval at <anonymous> (MetroClient.js:63), <anonymous>:29:22)
    at tryCallTwo (core.js:45)
    at doResolve (core.js:200)
    at new Promise (core.js:66)
    at insertNewConfig (eval at <anonymous> (MetroClient.js:63), <anonymous>:28:12)
    at Object.SelectInstanceScreen._this.continueLogin [as onPress] (eval at <anonymous> (MetroClient.js:63), <anonymous>:74:37)

似乎open() 函数必须作为函数调用(_constructor 必须是“函数”类型),但很明显open() 是作为函数调用的。 提前致谢。

【问题讨论】:

    标签: react-native mobile realm local-storage realm-mobile-platform


    【解决方案1】:

    我认为可能是上一个版本 (2.18.0) 的错误,尝试降级到 2.16.0 它将起作用。

    【讨论】:

      【解决方案2】:

      它与领域的最新版本(v2.18.0)相关。您应该降级到 2.16.0。

      之后,您可以运行此代码来重置所有包;

      watchman watch-del-all && rm -rf $TMPDIR/react-native-packager-cache-* && rm -rf $TMPDIR/metro-bundler-cache-* && rm -rf node_modules/ && yarn cache clean && yarn install && yarn start -- --reset-cache
      

      【讨论】:

      • 谢谢!因为这个,我暂时离开了Realm。但似乎这是上述版本本身的错误。
      【解决方案3】:

      我已经改变了 package.json 改变了

      来自"realm": "ˆ2.16.0",

      "realm": "2.16.0",

      然后执行npm installyarn

      帮我解决了。

      【讨论】:

        【解决方案4】:

        有一些与节点更新和领域相关的问题。 Realm 不适用于 Node 10,对于您的问题,降级到 2.16 将解决该问题。

        【讨论】:

          猜你喜欢
          • 2019-09-19
          • 2016-10-14
          • 2017-09-03
          • 1970-01-01
          • 2018-11-18
          • 2018-06-30
          • 1970-01-01
          • 2019-12-23
          • 1970-01-01
          相关资源
          最近更新 更多