`undefined is not a function` when using useObject
See original GitHub issueHow frequently does the bug occur?
All the time
Description
Hi !
I have a react-native project using "realm": "^11.1.0"
, "@realm/react": "^0.4.0"
and "react-native": "0.70.4"
.
I’m using useObject
to retrieve an object and it’s working fine until I try to access a property being an array of string.
Here’s what my code looks like
const ProjectComponent = () => {
const { params: { projectId} } = useRoute();
// Here project is { name: string, stringArray1: string[], stringArray2: string[], contracts: Contract[] }
const project = useObject(Project, new Realm.BSON.ObjectId(projectId));
const myString = project?.stringArray1?.[0] // The error happens here => undefined is not a function
Getting any project
value works fine even getting elements inside contracts
which is an array of contract objects but when trying to get elements inside stringArray1
or stringArray2
I get this error.
I looked at the stack trace and the problem seems to come from this line.
In my case object
is a string and object._objectKey
is not defined.
It works when I shallow copy the array because it is not using the proxy anymore.
Please let me know if I’m using realm or react-realm incorrectly or if you need more informations.
Stacktrace & log output
Can you reproduce the bug?
Yes, always
Reproduction Steps
To reproduce create a model with a property being an array of strings
export class Project extends Realm.Object {
_id;
name;
stringArray1;
stringArray2;
static schema = {
name: "Project",
properties: {
_id: "objectId",
name: "string",
stringArray1: "string[]",
stringArray2: { type: "string[]", default: [] },
},
primaryKey: "_id",
};
}
And inside a react component, retrieve an element of the array of string from this object
const project = useObject(Project, _id);
const myString = project?.stringArray1?.[0]
Version
11.1.0 and 11.0.0
What SDK flavour are you using?
Atlas Device Sync
Are you using encryption?
No, not using encryption
Platform OS and version(s)
Android 12
Build environment
I’m using Windows 10 version 21H2 to emulate (emulator version 31.2.10) a Pixel 4 phone running on Android 12
Issue Analytics
- State:
- Created a year ago
- Comments:6 (3 by maintainers)
@takameyer I confirm I no longer have an error thrown when accessing a value from an array of primitives. Thank you very much
@RenaudAubert I can see the issue in our current implementation. We are checking of the property of an object is an instance of
Realm.List
. Seemed there was a false assumption that aRealm.List
was a collection ofRealm.Object
. Therefore the code is trying to cache a list of primitives and assumes they areRealm.Object
s.We will look into this right away and update you here when a fix is available. Thanks for reporting this!