realm-js icon indicating copy to clipboard operation
realm-js copied to clipboard

Embedded Objects are returning 'undefined` in 10.20.0-beta.3

Open saravanakumargn opened this issue 2 years ago • 11 comments

How frequently does the bug occur?

All the time

Description

Embedded Objects are returning undefined in 10.20.0-beta.3. The same schema is working fine in 10.14.0.

Stacktrace & log output

No response

Can you reproduce the bug?

Yes, always

Reproduction Steps

Create any embedded Objects schema and check

Version

10.20.0-beta.3

What SDK flavour are you using?

Local Database only

Are you using encryption?

No, not using encryption

Platform OS and version(s)

React Native - iOS

Build environment

Which debugger for React Native: ..

Cocoapods version

No response

saravanakumargn avatar Apr 09 '22 12:04 saravanakumargn

Hi @saravanakumargn, thanks for the report. I was not able to reproduce your issue – I created a new react-native init project, enabled Hermes, installed [email protected] and then added some code to create an object with an embedded object and log it out. The result is as expected:

{"_id": "62544c283e892b3692cc8f01", "address": {"city": "city", "country": "country", "postalCode": "postalCode", "street": "street"}, "name": "name"}

Could you please create a minimal reproduction of your issue so we can help narrow down what is causing this? You could also try out my project (https://github.com/tomduncalf/realm-issue4484) to confirm you do not see the same issue there.

tomduncalf avatar Apr 11 '22 15:04 tomduncalf

@tomduncalf Thanks for your quick response. Now I can't reproduce this issue in your repo (Both Hermes enabled and disabled mode in iOS. Also, I tried using @realm/react). But, When I am running the same schema now I am getting the below results. I am Not sure what is wrong with my App. I don't have any special config in my App. (Created using npx react-native init)

I spent almost the whole day, reverted to 10.14.0, and raised this issue. Anyhow, I got the final results in 10.20.0-beta.3.

You can close this ticket, Once again thanks for your great support.

10.20.0-beta.3

 LOG  contact :  {"_id": "62545ef3b3cf33a950a011d6", "address": [undefined], "name": "name"}
 LOG  contact.address :  [undefined]
 LOG  contact.address[0] :  {"city": "city", "country": "country", "postalCode": "postalCode", "street": "street"}

10.15.0

 LOG  contact :  {"_id": "62545fece5decfee049bd1b3", "address": [[Object]], "name": "name"}
 LOG  contact.address :  [{"city": "city", "country": "country", "postalCode": "postalCode", "street": "street"}]
 LOG  contact.address[0] :  {"city": "city", "country": "country", "postalCode": "postalCode", "street": "street"}

schema

import {createRealmContext} from '@realm/react';

import {DownloadBooksSchema} from './DownloadBooksSchema';
import {NotesSchema} from './NotesSchema';

const AddressSchema = {
  name       : 'Address',
  embedded   : true, // default: false
  properties : {
    street     : 'string?',
    city       : 'string?',
    country    : 'string?',
    postalCode : 'string?'
  }
};
const ContactSchema = {
  name       : 'Contact',
  primaryKey : '_id',
  properties : {
    _id     : 'objectId',
    name    : 'string',
    // address: 'Address', // Embed a single object
    address : {
      type       : 'list',
      objectType : 'Address'
    }
  }
};

const myRealmConfig = {
  schema : [
    DownloadBooksSchema.schema,
    NotesSchema.schema,
    AddressSchema,
    ContactSchema
  ],
  ...(__DEV__ && {
    deleteRealmIfMigrationNeeded : true
  })
};

const {RealmProvider, useRealm, useQuery} = createRealmContext(myRealmConfig);

export {RealmProvider, useRealm, useQuery};

saravanakumargn avatar Apr 11 '22 17:04 saravanakumargn

Just to confirm @saravanakumargn, were you able to get this working with 10.20.0-beta.3 in the end? (Also, yesterday we released 10.20.0-beta.4)

tomduncalf avatar Apr 12 '22 08:04 tomduncalf

Yes @tomduncalf 10.20.0-beta.3 working fine for me now. Thanks for your kind support.

saravanakumargn avatar Apr 12 '22 08:04 saravanakumargn

Glad to hear it, I hope you enjoy Realm!

tomduncalf avatar Apr 12 '22 08:04 tomduncalf

@tomduncalf Now I am able to reproduce this issue in your repo. Seems, the issue with @realm/react and extends Realm.Object (use together).

Please check the code https://github.com/kalviapp/realm-issue4484. I am getting the below response.

 LOG  Contact :  [undefined]
 LOG  Contact 1 :  {"_id": "62556127710d74f1377f3bc5", "address": [undefined], "name": "name"}
 LOG  Contact 2 :  [undefined]
 LOG  Contact 3 :  {"city": "city", "country": "country", "postalCode": "postalCode", "street": "street"}

If this is out of scope please close this issue.

saravanakumargn avatar Apr 12 '22 11:04 saravanakumargn

Thanks for the repro, that was very helpful! I was able to isolate this to what seems like a Realm React bug: https://github.com/tomduncalf/realm-issue4484/commit/47848e9b75af69397d7f47a2fea9412d1c6b8e7e

With Realm React disabled, the embedded objects are populated, but with Realm React enabled, the embedded objects are undefined. We're currently working on improvements to Realm React, so we will look at this bug as part of them. I'll let you know when there is a new Realm React release for you to try!

tomduncalf avatar Apr 13 '22 10:04 tomduncalf

@takameyer FYI: It Seems, If I reverted to 10.15.0 it's working with @realm/react

saravanakumargn avatar Apr 14 '22 01:04 saravanakumargn

I observe a very similar behavior albeit not with embedded objects.

When loading a collection with useQuery, all elements are undefined unless I dereference them individually:

const cats = useQuery('Cat')

console.log(cats) // prints [undefined, undefined, undefined]
console.log(cat[0]) // prints { "id": 0, "name": "cat1" }
cats.forEach(console.log) // nothing prints at all!!
cats.map(console.log) // nothing as well!

for(let cat of cats) {
   console.log(cat)
} // properly prints all cats

I disabled hermes engine and installed realm@latest, this no longer happens.

mordechaim avatar May 11 '22 19:05 mordechaim

const cats = useQuery('Cat')

Array.from(cats) by using Array.from I solve this issue

PimiYvan avatar May 18 '22 16:05 PimiYvan

This issue seems to be addressed in [email protected]

mordechaim avatar Jul 28 '22 14:07 mordechaim

This issue seems to be addressed in [email protected]

I am closing the issue. Please create a new issue if the behaviour is still observed.

kneth avatar Oct 20 '22 12:10 kneth