realm / realm-js

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

'object type 'f' not found in schema' occurs when updating the application via code push appcenter #6777

Closed bac95ts closed 1 month ago

bac95ts commented 2 months ago

How frequently does the bug occur?

Always

Description

When updating the app release via CodePush on App Center, I encounter the error 'object type 'f' not found in schema'. This error does not occur when updating the app through TestFlight or the App Store. After updating bundle js from code push I see (use source map):

stack:

.../node_modules/realm/dist/ClassMap.js:132:26"
.../node_modules/realm/dist/ClassMap.js:156:33"
.../node_modules/realm/dist/Realm.js:677:58"
.../node_modules/@realm/react/dist/index.cjs:151:45"

Has anyone experienced a similar issue or can provide assistance?

const config: Realm.Configuration = {
  schema: [
    UserEntity,
     ...
  ],
  schemaVersion: 2,
  deleteRealmIfMigrationNeeded: true,
  onFirstOpen(realm: Realm) {
    UserEntity.createDefaultUsers(realm);
  },
};

export const {RealmProvider, useQuery, useObject, useRealm} =
  createRealmContext(config);

<RealmProvider path={`abc.realm`}>
      ...
</RealmProvider>

Stacktrace & log output

'Unhandled JS Exception: Error: Object type 'l' not found in schema.

This error is located at:
    in Unknown
    in Unknown
    in RCTView
    in Unknown
    in Unknown
    i..., stack:
value@1477:1091
value@1477:1740
value@1458:8114
p@1435:1644
anonymous@1435:2066
useMemo@239:50342
anonymous@24:6133
anonymous@1435:2043
anonymous@1912:273
Ir@239:43660
Ta@239:95329
yi@239:86333
vi@239:86235
mi@239:86000
ci@239:82953
pt@239:27146
ri@239:79605
ol@239:48909
C@1435:1393
anonymous@1482:1212
'
*** First throw call stack:
(
    0   CoreFoundation                      0x00000001804ae0f8 __exceptionPreprocess + 172
    1   libobjc.A.dylib                     0x0000000180087db4 objc_exception_throw + 56
    2   ChatSampleAppStaging                0x0000000104a53f1c RCTFormatError + 0
    3   ChatSampleAppStaging                0x00000001047def48 -[RCTExceptionsManager reportFatal:stack:exceptionId:extraDataAsJSON:] + 488
    4   ChatSampleAppStaging                0x00000001047df75c -[RCTExceptionsManager reportException:] + 1308
    5   CoreFoundation                      0x00000001804b4720 __invoking___ + 144
    6   CoreFoundation                      0x00000001804b1a84 -[NSInvocation invoke] + 276
    7   CoreFoundation                      0x00000001804b1d1c -[NSInvocation invokeWithTarget:] + 60
    8   ChatSampleAppStaging                0x0000000104a836b0 -[RCTModuleMethod invokeWithBridge:module:arguments:] + 388

Can you reproduce the bug?

Always

Reproduction Steps

No response

Version

"realm": 12.6.2

What services are you using?

Local Database only

Are you using encryption?

No

Platform OS and version(s)

ios

Build environment

Which release for React Native: ..

Cocoapods version

1.15.2

sync-by-unito[bot] commented 2 months ago

➤ PM Bot commented:

Jira ticket: RJS-2857

bac95ts commented 1 month ago

in file .../node_modules/realm/dist/ClassMap.js . I add comments as follows:

if (typeof arg === "string") {

            console.log(`Log 1 arg: ${arg}`);
            const mappingKeys = Object.keys(this.mapping);
            console.log(`Log 2 this.mapping.keys: ${JSON.stringify(mappingKeys)}`);

            const constructor = this.mapping[arg];
            if (!constructor) {
                throw new Error(`Object type '${arg}' not found in schema.`);
            }
            return constructor;
        }

After updating bundle code push:

Log 1 arg: l
Log 2 this.mapping.keys: ["UserEntity",...]

Error: Object type 'l' not found in schema
bac95ts commented 1 month ago

I think the problem is minify/obfuscator bundle Code Push process

bac95ts commented 1 month ago

I found the cause of this error, this configuration solved my problem. In metro.config.js, add minifierConfig like below:

// metro.config.js
const { getDefaultConfig } = require('metro-config');

module.exports = (async () => {
  const {
    resolver: { sourceExts, assetExts },
  } = await getDefaultConfig();
  return {
    transformer: {
      getTransformOptions: async () => ({
        transform: {
          experimentalImportSupport: false,
          inlineRequires: true,
        },
      }),
      minifierConfig: {
        mangle: {
          // Keep the names of the schemas to avoid errors
          keep_classnames: true,
          keep_fnames: true,
        },
        output: {
          comments: false,
        },
      },
    },
    resolver: {
      sourceExts: [...sourceExts, 'cjs'],
    },
  };
})();
kraenhansen commented 3 weeks ago

@bac95ts happy this got resoved! Thanks for updating the issue 👍