question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`undefined is not a function` when using useObject

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

2reactions
RenaudAubertcommented, 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

1reaction
takameyercommented, Nov 2, 2022

@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!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Uncaught TypeError: 'undefined' is not a function
It means that the expression did not return a function object. So you have to figure out what you are trying to execute...
Read more >
Working with objects - JavaScript - MDN Web Docs - Mozilla
A property's value can be a function, in which case the property is ... Nonexistent properties of an object have value undefined (and...
Read more >
Never use the Dot operator in JavaScript, for Data
This function will have two serious problems: What if the object you give the function is uninitialized at some point, undefined ? Then...
Read more >
Object methods, "this" - The Modern JavaScript Tutorial
Of course, we could use a pre-declared function as a method, like this: ... To tell the truth, the notations are not fully...
Read more >
TypeError undefined is not a function - Stack Overflow
What you want is the key-value pair from this object so that you can map those. This can be done with Object.entries() ....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found