3

Description

I'm trying to convert my realm object to an array as can be seen below in the history method.

class RealmStore {

    @observable symptoms = {};
    @observable meals    = {};

    @computed get history(){
        return [...Object.values(this.symptoms), ...Object.values(this.meals)];
    }

    //More methods to populate this.symptoms and this.meals
}

When I log this.symptoms I get the following output in my terminal:

{
  '0': {
    date: Fri Jun 29 2018 15: 56: 48 GMT + 0200(CEST),
    name: 'Regurgitation',
    value: 1
  },
  '1': {
    date: Fri Jun 29 2018 15: 58: 09 GMT + 0200(CEST),
    name: 'Belching',
    value: 1
  },
  '2': {
    date: Fri Jun 29 2018 16: 10: 39 GMT + 0200(CEST),
    name: 'Heartburn',
    value: 2
  },
  '3': {
    date: Fri Jun 29 2018 23: 30: 36 GMT + 0200(CEST),
    name: 'Heartburn',
    value: 1
  }
}

When I log Object.keys(this.symptoms) I get the following in my terminal:

[ '0', '1', '2', '3' ]

When I log Object.values(this.symptoms) I get the following in my terminal:

[]

This is the only way that this works:

        const values = [];
        for(let prop in this.symptoms){
            if(this.symptoms.hasOwnProperty(prop)){
                values.push(this.symptoms[prop])
            }
        }
        console.log(values);

This logs the following in my terminal:

[{
    date: Fri Jun 29 2018 15: 56: 48 GMT + 0200(CEST),
    name: 'Regurgitation',
    value: 1
  },
  {
    date: Fri Jun 29 2018 15: 58: 09 GMT + 0200(CEST),
    name: 'Belching',
    value: 1
  },
  {
    date: Fri Jun 29 2018 16: 10: 39 GMT + 0200(CEST),
    name: 'Heartburn',
    value: 2
  },
  {
    date: Fri Jun 29 2018 23: 30: 36 GMT + 0200(CEST),
    name: 'Heartburn',
    value: 1
  }
]

Question:

What is causing the realmjs object to be unable to return an array of values?

kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
  • Can you share the `observable` function please – baao Jun 29 '18 at 22:02
  • Can this be because of the `date` property? – bennygenel Jun 29 '18 at 22:06
  • @bambam I'm sorry, I don't understand your request to share the observable function as there is none? – kemicofa ghost Jun 29 '18 at 22:07
  • @bennygenel I was thinking the same thing, but it shouldn't be an issue since date is just a date object? It works with a for iterable loop. – kemicofa ghost Jun 29 '18 at 22:08
  • The observable decorator is a function that gets applied to the property. I just want to see if that does anything to the values. Is Object.values(this.meals) also empty? – baao Jun 29 '18 at 22:08
  • @bambam https://gist.github.com/rottenoats/7908b5c8d36ad47bb28c42f8e542a29a and yes it's empty as I haven't populated the meals in realm yet, so it's expected. – kemicofa ghost Jun 29 '18 at 22:14

2 Answers2

4

Currently unsure as to why Object.values() does not work. I went ahead and used this alternative, which according to some posts may cause performance issues.

Array.from(this.symptoms);
kemicofa ghost
  • 16,349
  • 8
  • 82
  • 131
0

For me this was due to inconsistent data types in the data available in Atlas. e.g. The date field in schema was of type 'Date' but some values in the collection were carrying the 'string' type. Having consistent data types solved the issue for me.