Closed saravanakumargn closed 3 years ago
Hi @saravanakumargn. Thanks for reporting this. We'll look into the issue. Is this a new issue in Realm JS 10.6.1, or have you seen it with previous versions?
@saravanakumargn I have created a fresh react-native app and implemented the source code you have provided. This works for me without a problem in iOS and Android. The only difference I could see is I am using Realm.BSON.UUID. How are you implementing UUID? Can you provide any other information? Perhaps a sample repository that reproduces this exact issue?
This is not new code and It was working fine before 10.5.0
and previous versions(My code base was older, and the same implementation I had since v10). Now I changed all my _id
s to 'uuid'. So, not able to revert older versions and test the same code.
@takameyer
Seems, Declaring schema using const is working fine. Using public static schema
not working.
This is my UUID implementation.
const {UUID} = Realm.BSON;
export function getUUID(input?: string | Buffer | undefined): UUIDType {
if (input) {
try {
return new UUID(input);
} catch ({message}) {
return new UUID();
}
}
return new UUID();
}
FYI. This is not because of Reanimated v2 (I had removed and tested). Also, Able to list items using realmRef.objects
.
@saravanakumargn Ah, are you calling new
on your Realm Model class? We do have some known issues involved with making a class extending "Realm.Object". Can you try declaring your class without the extension and doing creates like so:
realm.create<ObjectClass>('ObjectClass', value)
This may be a workaround until we fix Class Based Models.
@takameyer
After removing extends Realm.Object
able to create the new object and not able to convert results to JSON(). So reverted back I will continue my development in ios.
WARN Possible Unhandled Promise Rejection (id: 0):
TypeError: item.toJSON is not a function. (In 'item.toJSON(index.toString(), cache)', 'item.toJSON' is undefined)
@saravanakumargn the <T>
added to any realm query or mutation will add toJSON
(among other properties and functions of a realm object) to the return type. What is the line of code providing item
in this instance?
This is my list of objects.
const [listData, setListData] = useState<IBatchSchema[]>();
const batchList: Realm.Results<BatchSchema & Realm.Object> = realmRef
.objects<BatchSchema>(BatchSchema)
.filtered(query);
setListData(batchesData.toJSON());
@saravanakumargn try replacing .objects<BatchSchema>(BatchSchema)
with .objects<BatchSchema>('Batch')
. If the name of the model is not Batch
, then replace it with the correct schema name.
@takameyer
This is working perfectly. Able to create new records using .objects<BatchSchema>('Batch')
. But, still toJSON()
not working
@saravanakumargn I can reproduce this. We will check into it and let you know as soon as we find something.
This may be related to #3760.
@saravanakumargn So I have dug into this deeper. Are you opening your realm with your class or with your schema? If you are using the class, try changing to schema and that should fix the toJSON issue.
For example:
new Realm({schemas: [CategorySchema.schema]});
@takameyer
After removing
extends Realm.Object
able to create the new object and not able to convert results to JSON(). So reverted back I will continue my development in ios.WARN Possible Unhandled Promise Rejection (id: 0): TypeError: item.toJSON is not a function. (In 'item.toJSON(index.toString(), cache)', 'item.toJSON' is undefined)
I am facing exactly same issue.
@takameyer Do you have any idea/ETA about this fix in Android?
@saravanakumargn I cannot say for sure when we will have these classes refactored. Have you tried opening your Realm using the schema
object instead of the class? This should be a workaround to fix the TypeError
.
@takameyer EDIT: I was able to create in Both Android and IOS when I used the schema
object instead of the class as below code.
But when I use toJSON()
i am getting below error.
TypeError: item.toJSON is not a function. (In 'item.toJSON(index.toString(), cache)', 'item.toJSON' is undefined)
@takameyer Still I am facing the error only in Android. I have modified my code as below. Please help me to fix this and check is anything I missed?.
Error: Constructor was not registered in the schema for this Realm
My Code:
import Realm from 'realm';
import {
SettingsSchema,
ProfileSchema,
} from './schemas';
// prettier-ignore
const realmRef = new Realm(
{
schema : [
SettingsSchema.schema,
ProfileSchema.schema,
],
deleteRealmIfMigrationNeeded : __DEV__
}
);
export default realmRef;
import Realm from 'realm';
import {UUIDType} from '@src/types';
export interface ISettingsSchema {
languageCode: string;
isRTL?: Boolean;
}
export class SettingsSchema extends Realm.Object implements ISettingsSchema {
public _id: UUIDType;
public languageCode: string;
public isRTL?: Boolean;
public static schema: Realm.ObjectSchema = {
name : 'Settings',
primaryKey : '_id',
properties : {
_id : 'uuid',
languageCode : 'string',
isRTL : {
type : 'bool?',
default : false
}
}
};
}
Reading from SettingsSchema
. Working fine in Android & iOS
const settings = realmRef.objects<SettingsSchema>('Settings')[0];
function saveProfile(values: ISettingsSchema) {
try {
realmRef.write(() => {
realmRef.create<SettingsSchema>('Settings', values, Realm.UpdateMode.Modified);
});
navigation.goBack();
} catch ({message}) {
// eslint-disable-next-line no-console
console.error(message);
}
}
I think your non-optional class members should be declared using !
.
For example:
public _id!: UUIDType;
public languageCode!: string;
Other than that, the code looks correct and should be able to use toJSON
. What is your implementation of toJSON
? If you are querying realm using a string instead of the model class, than toJSON
should be defined.
@takameyer
My issue got resolved. This is because of react-native-animatable
v2. I didn't enable enableHermes
, in my build.gradle still enableHermes: false
. After downgraded react-native-animatable
to v1, this undefined issue got resolved with your solution.
Thanks for your all support. really appreciate it.
Android getting an empty object, where iOS getting results of the first object. Because of empty object toJSON() is failing.
function loadProfile() {
const profileResults = realmRef.objects<ProfileSchema>('Profile').sorted('fullName');
console.log(profileResults?.length) // 1
console.log(profileResults?.[0]) // {}
// setListData(profileResults.toJSON());
}
@saravanakumargn Thanks for following up on this. Happy that you were able to get further along. I will now close this issue
Still facing the same issue, with @realm/react and the hooks useRealm, useQuery.
@tejasNix without @realm/react
is this working fine?
@takameyer
The problem still persists on Android because of react-native-reanimatedV2
"react-native": "^0.70.0", "realm": "^10.21.1", "react-native-reanimated": "^2.10.0", "@realm/react": "^0.3.2",
@theBeesAtWork This should work with:
"realm": "11.0.0-rc.2"
Thank you so much. You saved my life @takameyer. It works now with "realm": "11.0.0-rc.2"
Still having the same error.
I just used this template : npx expo-cli init ReactRealmJSTemplateApp -t @realm/expo-template-js
I updated my package.json file to be able to use realm": "11.0.0-rc.2
Now it's even worse
First Error: android/app/build.gradle:
@ismailsemihsenturk Please check your dependencies against https://github.com/realm/realm-js/blob/master/COMPATIBILITY.md
Goals
When I try to use
objectForPrimaryKey
orobjects
it's working as expected in Both iOS and Android. like as below:Expected Results
No issues while saving in iOS. Should as fine in Android like iOS.
Actual Results
In Android it's throwing {{ ERROR RealmObject cannot be called as a function}} on Create.
Steps to Reproduce
Code Sample
Version of Realm and Tooling