Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TypeError: Cannot read property 'size' of undefined #6626

Open
ronkorland opened this issue Apr 17, 2024 · 27 comments
Open

TypeError: Cannot read property 'size' of undefined #6626

ronkorland opened this issue Apr 17, 2024 · 27 comments

Comments

@ronkorland
Copy link

How frequently does the bug occur?

Always

Description

realm: 12.6.2
@realm/react: 0.6.2

I created one react-native page inside react-router; the page uses the useQuery hook to query my model.
When I open the page, I always get the following error:
TypeError: Cannot read property 'size' of undefined

Stacktrace & log output

get (Results.js:62)
getOwnPropertyDescriptor (OrderedCollection.js:62)
set (OrderedCollection.js:51)
Results (Results.js:33)
objects (Realm.js:691)
getObjects (index.cjs:151)
anonymous (index.cjs:182)
mountMemo (ReactNativeRenderer-dev.js:11819)
useMemo (ReactNativeRenderer-dev.js:12275)
useMemo (react.development.js:1650)
useQuery (index.cjs:181)
RealmDbPage (MainPage.tsx:30)
renderWithHooks (ReactNativeRenderer-dev.js:10867)
mountIndeterminateComponent (ReactNativeRenderer-dev.js:14789)
beginWork (ReactNativeRenderer-dev.js:16628)
beginWork$1 (ReactNativeRenderer-dev.js:22324)
performUnitOfWork (ReactNativeRenderer-dev.js:21471)
workLoopSync (ReactNativeRenderer-dev.js:21398)
renderRootSync (ReactNativeRenderer-dev.js:21369)
performSyncWorkOnRoot (ReactNativeRenderer-dev.js:21020)
flushSyncCallbacks (ReactNativeRenderer-dev.js:6262)
flushSyncCallbacksOnlyInLegacyMode (ReactNativeRenderer-dev.js:6241)
batchedUpdates$1 (ReactNativeRenderer-dev.js:21076)
batchedUpdates (ReactNativeRenderer-dev.js:2750)
_receiveRootNodeIDEvent (ReactNativeRenderer-dev.js:2892)
receiveTouches (ReactNativeRenderer-dev.js:3012)
__callFunction (MessageQueue.js:433)
anonymous (MessageQueue.js:113)
__guard (MessageQueue.js:365)
callFunctionReturnFlushedQueue (MessageQueue.js:112)

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

12.6.2

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

Android 13

Build environment

Which debugger for React Native: Flipper

Cocoapods version

No response

Copy link

sync-by-unito bot commented Apr 17, 2024

➤ PM Bot commented:

Jira ticket: RJS-2805

@kneth
Copy link
Contributor

kneth commented Apr 22, 2024

@ronkorland Please provide a reproduction case or at least a stack trace.

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Apr 22, 2024
@ronkorland
Copy link
Author

@kneth the following stack trace is not enough?

get (Results.js:62)
getOwnPropertyDescriptor (OrderedCollection.js:62)
set (OrderedCollection.js:51)
Results (Results.js:33)
objects (Realm.js:691)
getObjects (index.cjs:151)
anonymous (index.cjs:182)
mountMemo (ReactNativeRenderer-dev.js:11819)
useMemo (ReactNativeRenderer-dev.js:12275)
useMemo (react.development.js:1650)
useQuery (index.cjs:181)
RealmDbPage (MainPage.tsx:30)
renderWithHooks (ReactNativeRenderer-dev.js:10867)
mountIndeterminateComponent (ReactNativeRenderer-dev.js:14789)
beginWork (ReactNativeRenderer-dev.js:16628)
beginWork$1 (ReactNativeRenderer-dev.js:22324)
performUnitOfWork (ReactNativeRenderer-dev.js:21471)
workLoopSync (ReactNativeRenderer-dev.js:21398)
renderRootSync (ReactNativeRenderer-dev.js:21369)
performSyncWorkOnRoot (ReactNativeRenderer-dev.js:21020)
flushSyncCallbacks (ReactNativeRenderer-dev.js:6262)
flushSyncCallbacksOnlyInLegacyMode (ReactNativeRenderer-dev.js:6241)
batchedUpdates$1 (ReactNativeRenderer-dev.js:21076)
batchedUpdates (ReactNativeRenderer-dev.js:2750)
_receiveRootNodeIDEvent (ReactNativeRenderer-dev.js:2892)
receiveTouches (ReactNativeRenderer-dev.js:3012)
__callFunction (MessageQueue.js:433)
anonymous (MessageQueue.js:113)
__guard (MessageQueue.js:365)
callFunctionReturnFlushedQueue (MessageQueue.js:112)

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Apr 22, 2024
@Mitch528
Copy link

I have the same issue. Seems to have happened after upgrading to react-native 0.74.

@ronkorland
Copy link
Author

I'm using react-native: 0.73.6 but I have the same issue

@Mitch528
Copy link

I'm using react-native: 0.73.6 but I have the same issue

@ronkorland

do you have this@babel/plugin-transform-private-methods in your babel config? That seems to have been the cause, at least it was for me.

I fixed it by removing it and adding this to my config instead:

  overrides: [
    {
      exclude: /node_modules\/realm\//,
      plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
    },
  ],

@ronkorland
Copy link
Author

I'm using react-native: 0.73.6 but I have the same issue

@ronkorland

do you have this@babel/plugin-transform-private-methods in your babel config? That seems to have been the cause, at least it was for me.

I fixed it by removing it and adding this to my config instead:

  overrides: [
    {
      exclude: /node_modules\/realm\//,
      plugins: [['@babel/plugin-transform-private-methods', { loose: true }]],
    },
  ],

Thanks for reply, me babel config looks like this:

module.exports = {
  presets: [["module:@react-native/babel-preset"]],
  env: {
    production: {
      plugins: ["react-native-paper/babel"],
    },
  },
  plugins: [
    "@realm/babel-plugin",
    "babel-plugin-transform-typescript-metadata",
    ["@babel/plugin-proposal-decorators", { legacy: true }],
    ["@babel/plugin-transform-class-properties", { loose: true }],
    ["@babel/plugin-transform-private-property-in-object", { loose: true }],
  ],
  overrides: [
    {
      exclude: /node_modules\/realm\//,
      plugins: [["@babel/plugin-transform-private-methods", { loose: true }]],
    },
  ],
};

But I still have the same error.
untitled

@Mitch528
Copy link

Mitch528 commented Apr 30, 2024

@ronkorland

did you reset the cache after editing the config file?

react-native start --reset-cache

@ronkorland
Copy link
Author

@Mitch528 I try now with the reset cache but I still have the same error

@vijaykharage7
Copy link

Hello @ronkorland did you happen to resolve this issue?

@ronkorland
Copy link
Author

ronkorland commented May 10, 2024 via email

@alexandrubichis
Copy link

Hello. Is any update on this issue?

@alexandrubichis
Copy link

Hello. After upgrading react-native from 0.73.5 to 0.74.1 I encounter the same error. My error was because of module:metro-react-native-babel-preset from babel config preset instead of module:@react-native/babel-preset. No need to install @babel/plugin-transform-private-methods package because is installed as dependency from @babel/preset-env.

package.json:

{
  "name": "Test",
  "version": "0.0.1",
  "private": true,
  "scripts": {
    "android": "react-native run-android",
    "ios": "react-native run-ios",
    "lint": "eslint .",
    "start": "react-native start",
    "test": "jest"
  },
  "dependencies": {
    "react": "18.2.0",
    "react-native": "0.74.1",
    "realm": "12.6.2"
  },
  "devDependencies": {
    "@babel/core": "^7.20.0",
    "@babel/preset-env": "^7.20.0",
    "@babel/runtime": "^7.20.0",
    "@react-native/babel-preset": "0.74.83",
    "@react-native/eslint-config": "0.74.83",
    "@react-native/metro-config": "0.74.83",
    "@react-native/typescript-config": "0.74.83",
    "@types/react": "^18.2.6",
    "@types/react-test-renderer": "^18.0.0",
    "babel-jest": "^29.6.3",
    "eslint": "^8.19.0",
    "jest": "^29.6.3",
    "prettier": "2.8.8",
    "react-test-renderer": "18.2.0",
    "typescript": "5.0.4"
  },
  "engines": {
    "node": ">=18"
  }
}

babel.config.js:

module.exports = {
  presets: ['module:@react-native/babel-preset'],
};

@davide-ciraolo
Copy link

Hello everyone, I've tried to figure it out this problem in last days but without success so far. The only thing that did work for me was to modify the code of the module from:

...
get length() {
    return this.internal.size();
}
...

to:

...
get length() {
    return this.internal ? this.internal.size() : 0;
}
...

in node_modules/realm/dist/bundle.react-native.js file.
Strangely enough, trying the official expo-example (https://github.com/realm/realm-js/tree/main/templates/expo-template), the library does work without issues. I've tried to configure my project with the exact same packages but the issue was still there until I made this change. I don't know if the issue is really code-related, but I think the target file to update to make this change is this one https://github.com/realm/realm-js/blob/v12.0.0/packages/realm/src/Results.ts (lines 78 to 80). Let me know what you think about it.

@kneth
Copy link
Contributor

kneth commented May 30, 2024

@davide-ciraolo You have a couple of interesting observations, and I am curious to understand the impact of Babel is (#6626 (comment) is hinting that Babel might have a negative impact). Can you share your Babel configuration?

@davide-ciraolo
Copy link

Hello @kneth yesterday after putting in place my "temporary fix" I continued developing my application and had another problem with the FlastList component that I ended up fixing following this answer: facebook/react-native#36828 (comment). Today I removed my "temporary fix" and after resetting the cache with npx expo start --reset-cache I tested the app again, and Realm worked without problems.
Probably, this means that installing @babel/plugin-transform-flow-strip-types (with npm install @babel/plugin-transform-flow-strip-types --save-dev or yarn add -D @babel/plugin-transform-flow-strip-types) and putting it on the first line of babel plugins configuration fixed this issue. Here is my actual babel configuration:

module.exports = function(api) {
  api.cache(true);
  return {
    presets: ['babel-preset-expo'],
    plugins: [
      '@babel/plugin-transform-flow-strip-types',
      ["@babel/plugin-transform-private-methods", { "loose": true }],
      ["@babel/plugin-transform-class-properties", { "loose": true }],
      ["@babel/plugin-transform-private-property-in-object", { "loose": true }],
      "@realm/babel-plugin", ["@babel/plugin-proposal-decorators", { legacy: true}],
      [
        'module-resolver',
          {
            alias: {
              'rn-crypto': 'react-native-quick-crypto',
              'stream': 'stream-browserify',
              'buffer': '@craftzdog/react-native-buffer',
            }
          }
      ]
    ]
  };
};

I hope it will be helpful for anyone having the same issue, although I'm not sure what's going on with babel under the hood. In fact, the expo-realm example works without using the @babel/plugin-transform-flow-strip-types as you can see in the example's babel configuration:

module.exports = function (api) {
  api.cache(true);
  return {
    presets: ["babel-preset-expo"],
    plugins: ["@realm/babel-plugin", ["@babel/plugin-proposal-decorators", { legacy: true }]],
  };
};

@Weverson-Luan
Copy link

Weverson-Luan commented Jun 14, 2024

Olá a todos, tentei resolver esse problema nos últimos dias, mas sem sucesso até agora. A única coisa que funcionou para mim foi modificar o código do módulo de:

...
get length() {
    return this.internal.size();
}
...

pára:

...
get length() {
    return this.internal ? this.internal.size() : 0;
}
...

no arquivo node_modules/realm/dist/bundle.react-native.js. Estranhamente, tentando o expo-example oficial ( https://github.com/realm/realm-js/tree/main/templates/expo-template ), a biblioteca funciona sem problemas. Tentei configurar meu projeto exatamente com os mesmos pacotes, mas o problema ainda persistia até que fiz essa alteração. Não sei se o problema está realmente relacionado ao código, mas acho que o arquivo de destino está atualizado para fazer essa alteração é este https://github.com/realm/realm-js/blob/v12.0.0/packages /realm /src/Results.ts (linhas 78 a 80). Deixe-me saber o que você pensa sobre isso.

solution valid for min using react-native-cli in version v0.74.2 🚀

@ApacheEx
Copy link

ApacheEx commented Jun 24, 2024

hmm, facing the same issue

I've resolved it by adding exclude: /node_modules\/realm\// into babel config

as I see, the main issue was having this plugin'@babel/plugin-proposal-private-methods'

@ronkorland
Copy link
Author

hmm, facing the same issue

I've resolved it by adding exclude: /node_modules\/realm\// into babel config

as I see, the main issue was having this plugin'@babel/plugin-proposal-private-methods'

can you please share your babel config settings?

@ApacheEx
Copy link

@ronkorland here it is

module.exports = {
  presets: [
    '@babel/preset-env',
    '@babel/preset-typescript',
    '@babel/preset-react',
    'module:@react-native/babel-preset',
    '@babel/preset-flow',
  ],
  plugins: [
    '@babel/plugin-transform-react-jsx',
    '@babel/plugin-syntax-class-properties',
    '@babel/plugin-proposal-nullish-coalescing-operator',
    ['@babel/plugin-proposal-private-methods', { loose: true }],
    ['@babel/plugin-transform-private-property-in-object', { loose: true }],
  ],
  exclude: /node_modules\/realm\//,
};

@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Jul 22, 2024
@ShaileshRajesh
Copy link

@ApacheEx after adding exclude: /node_modules/realm//, into babel config when I run my application my app is crashing can you please help me

@kneth
Copy link
Contributor

kneth commented Aug 5, 2024

@ShaileshRajesh

my app is crashing

Can you provide the error message and a stack trace?

@tekvyy
Copy link

tekvyy commented Aug 7, 2024

@kneth
On excluding realm from babel.config.js I get the below error

Error:
ReferenceError: Property 'RealmInExpoGoError' doesn't exist, js engine: hermes

Babel Config:

  presets: [
    'module:@react-native/babel-preset',
  ],
  plugins: [
    [
      'module:react-native-dotenv',
      {
        moduleName: '@env',
        path: '.env',
      },
    ],
    '@babel/plugin-transform-react-jsx',
    '@babel/plugin-syntax-class-properties',
    '@babel/plugin-proposal-nullish-coalescing-operator',
    ['@babel/plugin-proposal-private-methods', { loose: true }],
    ['@babel/plugin-transform-private-property-in-object', { loose: true }],
  ],
  exclude: /node_modules\/realm\//,
};```

@tekvyy
Copy link

tekvyy commented Aug 7, 2024

Removing the plugin

['@babel/plugin-proposal-private-methods', { loose: true }],

Fixed my issue.

@kneth
Copy link
Contributor

kneth commented Aug 7, 2024

@tekvyy

The plugin is deprecated so I assume it is safe to remove it 😄

@arthurkaizer
Copy link

Hi everyone,

I encountered a similar issue with Babel configuration in my React Native project using Realm, and I found a solution that worked for me:

module.exports = {
  presets: ['module:metro-react-native-babel-preset'],
  plugins: [
    ['@babel/plugin-proposal-decorators', { legacy: true }],
    ['@babel/plugin-transform-private-methods', { loose: true }],
],
};

@ohhyunjin
Copy link

Hi everyone, are there any updates on this one?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.