realm-js
realm-js copied to clipboard
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
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
@djgoulart I have created a project with the same versions and toJSON
is functioning. Can you verify this project runs on your computer?
https://github.com/takameyer/stunning-palm-tree
The toJSON line is here: https://github.com/takameyer/stunning-palm-tree/blob/main/app/AppNonSync.tsx#L12
@takameyer Sorry for the delay. I can confirm that your example is running without problems on sync and non-sync mode.
I can't determine what Im doing wrong on my side, but I will review all my code to find the issue.
@djgoulart I'm curious what value
is in extensions.js
. Could you perhaps add some logs in node_modules/realm/lib/extensions.js
around line 115 to see what the value is when the error is triggered?
@djgoulart I'm curious what
value
is inextensions.js
. Could you perhaps add some logs innode_modules/realm/lib/extensions.js
around line 115 to see what the value is when the error is triggered?
@takameyer For sure! I will do that and return with the results.
@djgoulart Any updates?
Having the same problem. Using last version.
To add about what @takameyer requested: I added several logs in extensions.js,
module.exports = function (realmConstructor) {
Is executed.
Object.defineProperty(realmConstructor.Collection.prototype, "toJSON", {
Is executed.
Object.defineProperty(realmConstructor.Object.prototype, "toJSON", {
Is executed.
This code:
export const getStoreFromDb = async (
id: ObjectId,
): Promise<Store | undefined> => {
const realm: Realm = await getRealm();
const result = realm.objectForPrimaryKey<Store>('Store', id);
if (!result) {
return undefined;
}
console.log('[tmp] result', result, result instanceof Realm.Object);
return result.toJSON();
};
- The object exists on realm.
- result instanceof Realm.Object is TRUE
- toJSON is undefined:
[TypeError: result.toJSON is not a function. (In 'result.toJSON()', 'result.toJSON' is undefined)]
Tried the hermes version: "realm": "^11.0.0-rc.0", enabled Hermes and now it's working. No idea why but I guess it's something.
same issue
@d1nhnh4n We will appreciate if you can create a small project which can reproduce it.
In v11.0.0 we added toJSON()
and it can handle cyclic object graphs. The output can be used together with packages like flatted
.