Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

undefined is not a function when using useObject #5058

Closed
RenaudAubert opened this issue Nov 2, 2022 · 6 comments · Fixed by #5069
Closed

undefined is not a function when using useObject #5058

RenaudAubert opened this issue Nov 2, 2022 · 6 comments · Fixed by #5069

Comments

@RenaudAubert
Copy link

RenaudAubert commented Nov 2, 2022

How 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

Capture d’écran 2022-11-02 134332

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

@kneth
Copy link
Member

kneth commented Nov 2, 2022

@RenaudAubert

Another user reported the same thing but an update solved the issue. Can it be an issue with a build cache?

@sync-by-unito sync-by-unito bot added the Waiting-For-Reporter Waiting for more information from the reporter before we can proceed label Nov 2, 2022
@RenaudAubert
Copy link
Author

RenaudAubert commented Nov 2, 2022

@kneth Hi thanksfor the quick reply.
I saw this issue but I'm not sure it's related.
The error comes from _objectKey which means I'm up to date with @realm/react and I've started this project using @realm/react: 0.4.0 and "realm": "^11.0.0"

The problem here is that the _objectKey function is called on a string.

@github-actions github-actions bot added Needs-Attention Reporter has responded. Review comment. and removed Waiting-For-Reporter Waiting for more information from the reporter before we can proceed labels Nov 2, 2022
@takameyer
Copy link
Contributor

@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 a Realm.List was a collection of Realm.Object. Therefore the code is trying to cache a list of primitives and assumes they are Realm.Objects.

We will look into this right away and update you here when a fix is available. Thanks for reporting this!

@RenaudAubert
Copy link
Author

@takameyer Thank you 🙏
Let me know if you need more information regarding this issue

@takameyer takameyer mentioned this issue Nov 3, 2022
11 tasks
@sync-by-unito sync-by-unito bot removed the Needs-Attention Reporter has responded. Review comment. label Nov 3, 2022
@takameyer
Copy link
Contributor

@RenaudAubert This should be fixed now. Please use @realm/react@0.4.1

@RenaudAubert
Copy link
Author

RenaudAubert commented Nov 4, 2022

@takameyer I confirm I no longer have an error thrown when accessing a value from an array of primitives. Thank you very much

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Mar 15, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants