Uploaded image for project: 'Realm JavaScript SDK'
  1. Realm JavaScript SDK
  2. RJS-1784

Realm.Results<Realm.Object> .toJSON() is not working

      How frequently does the bug occur?

      All the time

      Description

      The problem:

      When i try to convert the result of : realm.objects("MyModel") to JSON object, it doesn't work and throw an error.

      useEffect(() => {
          const formsResult = realm.objects("Form");
          console.log(formsResult.toJSON());
          console.log(JSON.stringify(formsResult)); //also doesn't works
          
        }, []);
      

      Most relevant dependencies (Check the full list in the reprodution steps)

       "dependencies": {
          "@realm/react": "^0.3.2",
          "expo": "^44.0.6",
          "react": "17.0.1",
          "react-native": "0.64.3",
          "realm": "^10.19.5",
        },
      

      Logs in the emulator

      Stacktrace & log output

      Unable to find source-code formatter for language: shell. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      TypeError: value.toJSON is not a function. (In 'value.toJSON(key, cache)', 'value.toJSON' is undefined)
      
      This error is located at:
          in UserStatisticsProvider (created by AppContextProvider)
          in AppContextProvider (created by RealmAppWrapper)
          in Unknown (created by AppSync)
          in AppSync (created by RealmAppWrapper)
          in UserProvider (created by RealmAppWrapper)
          in AppProvider (created by RealmAppWrapper)
          in ThemeProvider (created by RealmAppWrapper)
          in GestureHandlerRootView (created by GestureHandlerRootView)
          in GestureHandlerRootView (created by RealmAppWrapper)
          in RealmAppWrapper (created by ExpoRoot)
          in ExpoRoot
          in RCTView (created by View)
          in View (created by AppContainer)
          in RCTView (created by View)
          in View (created by AppContainer)
          in AppContainer
      at node_modules/react-native/Libraries/Core/ExceptionsManager.js:104:6 in reportException
      at node_modules/react-native/Libraries/Core/ExceptionsManager.js:172:19 in handleException
      at node_modules/react-native/Libraries/Core/setUpErrorHandling.js:24:6 in handleError
      at node_modules/expo-dev-launcher/build/DevLauncherErrorManager.js:44:19 in errorHandler
      at node_modules/expo-dev-launcher/build/DevLauncherErrorManager.js:49:24 in <anonymous>
      at node_modules/expo-error-recovery/build/ErrorRecovery.fx.js:12:21 in ErrorUtils.setGlobalHandler$argument_0
      

      Can you reproduce the bug?

      Yes, always

      Reproduction Steps

      Realm Sync

      I'm using partition sync.

      The code for reproduce this error is

      useEffect(() => {
          const formsResult = realm.objects("Form");
          console.log(formsResult.toJSON());
          console.log(JSON.stringify(formsResult)); //also doesn't works
          
        }, []);
      

      My model file is configured like the code bellow:

      class Form extends Realm.Object {
        _id?: Realm.BSON.ObjectId;
        _partition: string;
        active?: boolean;
        author?: Author;
        name: string;
        sections: Section[];
        createdAt: Date;
        updatedAt: Date;
      
        static schema: Realm.ObjectSchema = {
          name: 'Form',
          properties: {
            _id: 'objectId?',
            _partition: 'string',
            active: 'bool?',
            author: 'Author',
            name: 'string',
            sections: 'Section[]',
            createdAt: 'date?',
            updatedAt: 'date?'
          },
          primaryKey: '_id',
        }
      }
      

      The stack i'm using:

      {
        "scripts": {
          "start": "expo start --dev-client",
          "android": "expo run:android",
          "ios": "expo run:ios"
        },
        "dependencies": {
          "@expo-google-fonts/ubuntu": "^0.2.2",
          "@fortawesome/fontawesome-svg-core": "^6.1.1",
          "@fortawesome/free-solid-svg-icons": "^6.1.1",
          "@fortawesome/pro-light-svg-icons": "^6.1.1",
          "@fortawesome/react-native-fontawesome": "^0.2.7",
          "@gorhom/portal": "^1.0.13",
          "@hookform/resolvers": "^2.9.0",
          "@react-native-community/netinfo": "7.1.3",
          "@react-navigation/bottom-tabs": "^6.3.1",
          "@react-navigation/native": "^6.0.10",
          "@react-navigation/native-stack": "^6.6.2",
          "@realm/react": "^0.3.2",
          "expo": "^44.0.6",
          "expo-blur": "~11.0.0",
          "expo-dev-client": "~0.8.5",
          "expo-font": "~10.0.4",
          "expo-screen-orientation": "~4.1.1",
          "expo-splash-screen": "~0.14.2",
          "expo-status-bar": "~1.2.0",
          "expo-updates": "~0.11.6",
          "react": "17.0.1",
          "react-dom": "17.0.1",
          "react-hook-form": "^7.31.3",
          "react-native": "0.64.3",
          "react-native-gesture-handler": "~2.1.0",
          "react-native-get-random-values": "~1.7.2",
          "react-native-picker-select": "^8.0.4",
          "react-native-responsive-fontsize": "^0.5.1",
          "react-native-safe-area-context": "3.3.2",
          "react-native-screens": "~3.10.1",
          "react-native-svg": "12.1.1",
          "react-native-web": "0.17.1",
          "realm": "^10.19.5",
          "styled-components": "^5.3.5",
          "yup": "^0.32.11"
        },
        "devDependencies": {
          "@babel/core": "^7.12.9",
          "@types/react": "~17.0.21",
          "@types/react-native": "~0.64.12",
          "@types/styled-components-react-native": "^5.1.3",
          "react-native-svg-transformer": "^1.0.0",
          "typescript": "~4.3.5"
        },
        "resolutions": {
          "@types/react": "^17"
        },
        "name": "edify-forms",
        "version": "1.0.0",
        "private": true
      }
      

      Version

      10.19.5

      What SDK flavour are you using?

      Atlas Device Sync

      Are you using encryption?

      No, not using encryption

      Platform OS and version(s)

      Android SDK 32

      Build environment

      Which debugger for React Native: Expo EAS builder.

      Cocoapods version

      No response

            Assignee:
            andrew.meyer@mongodb.com Andrew Meyer
            Reporter:
            unitosyncbot Unito Sync Bot
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: