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

Realm.Results.map is not a function? #2001

Closed
pedrosimao opened this issue Aug 30, 2018 · 14 comments
Closed

Realm.Results.map is not a function? #2001

pedrosimao opened this issue Aug 30, 2018 · 14 comments

Comments

@pedrosimao
Copy link

pedrosimao commented Aug 30, 2018

Goals

I did query a Realm collection

Expected Results

I expect to be able to execute .map() or .forEach() methods on Realm results.

Actual Results

When I console.log() the realm results I see well that the type of the object is a result.
But when I do try to execute .map() method on it I get the following error:
TypeError: schedules.map is not a function

Code Sample

let selected = realm.objects('SchemaName');
selected.map(selection => .... )

Version of Realm and Tooling

  • Realm JS SDK Version: ?
    2.15.3
  • Node or React Native: ?
    RN: 0.56.0
  • Client OS & Version: ?
    Mac OSx
@pedrosimao
Copy link
Author

I just found a workaround this issue.

Instead of using map() function, I just needed to find the selected.length, and use it inside a for loop, like this:

for (let i = 0; i < selected.length; i++){...
selected[i] ...}

An implementation of map or forEach methods would be a great plus.

@pedrosimao
Copy link
Author

Given that I have found a solution I will close the issue.
But developers should really think about include some modern Javascript methods on Realm.objects and Realm.results.
RealmJS should follow the tendencies of modern javascript syntax... Also this kind of operations could be better documented.

@nirinchev
Copy link
Member

@kneth is this a RN bug? I am fairly certain we've used .map with node apps.

@pedrosimao
Copy link
Author

Well, it could be. I have noticed that running the same code on Simulator + Debugger and on a real iOs device sometimes give me different results. Anyway on RN it didn't work for me in any of the cases. Maybe this issue is something to be looked on. Should I reopen it?

@nirinchev nirinchev reopened this Aug 31, 2018
@nirinchev
Copy link
Member

Yes, let's check it out.

@pedrosimao
Copy link
Author

Quick update, it seems on React Native it is possible to use .map or .forEach methods like this:
Object.keys(object).forEach((key) => ... do something with key )
Chaining the method directly on the object like object.forEach(key => ...) will cause the error described.
I am not sure it is supposed to be like that. If it is the case it would be great to see that in the docs.

@kneth
Copy link
Contributor

kneth commented Oct 24, 2018

@Udbhav12
Copy link

Udbhav12 commented Mar 14, 2019

I am facing this exact issue in RN 0.58.6 and above,It was working fine in RN 0.57.8 and below

@febeling
Copy link

Confirming this problem for react-native@0.55.3

@febeling
Copy link

Also see this with Realm.List, not just Realm.Results.

@Rob117
Copy link

Rob117 commented Oct 15, 2019

Just to be clear, this is still an issue. When you search for realm node foreach, this comes up on the first page.

If you check all the methods that should be available on the object using (taken from SO)

    const getMethods = obj => {
      let properties = new Set();
      let currentObj = obj;
      do {
        Object.getOwnPropertyNames(currentObj).map(item => properties.add(item));
      } while ((currentObj = Object.getPrototypeOf(currentObj)));
      return [...properties.keys()].filter(item => typeof obj[item] === 'function');
    };

  console.log(getMethods(resultsObject));

This will tell you that forEach should be on that object, but trying to actually call it returns not a function

@davidleee
Copy link

davidleee commented Oct 17, 2019

We've spotted the same error on our Electron app.

I know Realm does not currently support Electron, but maybe this is not an RN-only issue?

@kneth
Copy link
Contributor

kneth commented Oct 17, 2019

It can be related to #1096.

@kneth
Copy link
Contributor

kneth commented Jul 13, 2023

We believe this is fixed by v12. Please reopen or create a new issue if it is still a problem.

@kneth kneth closed this as completed Jul 13, 2023
@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

No branches or pull requests

10 participants