The method "getProperty" of "helper/object" can't deal with null

Tags: #<Tag:0x00007fbe8fee6718>

Hi,

Our data format could be:

data: {
  test1: {
    test2: 'test'
  }
}

To show the value of property ‘test2’, we set the data to test1.test2.
However, the above data could be

data: {
  test1: null
}

And, in this case, the getProperty throws the error because data.test1 is null.
Can getProperty return false in case the value is null as well?

Hi @lijigang.us

The getProperty is not an officially supported method.

Could you tell me the purpose of this action? When do you want to look for properties of the test1

  • load the data,
  • change the data,
  • metadata logic
  • use in hooks

Hi @aleksandra_budnik

Apologize. I should have post the jsfiddle in the first place.
https://jsfiddle.net/guobacai/j8xrLpe5/10/
I think this should be the most basic usage of rendering a simple table.
In this fiddle, you can see the table isn’t rendered correctly. That is because one of the profile in the data is null. If its value is changed to undefined. The table can render correctly.
In the dev-tools console, it shows the error as well.

So, I wonder whether the getProperty should deal with null as well since it already consider the undefined.

Thanks so much.

I’m afraid that Handsontable currently does not support this type of data. We support array of arrays [[],[]] and arrays of objects [{}, {}]. Your data is nested, it has an additional level of objects[{}, {{}}].
I suggest parsing your dataset to a 2D structure.

Hi @aleksandra_budnik

Thanks for your quick response.

In the official document, it looks to me that Handsontable supports the nested object.
https://handsontable.com/docs/7.4.2/tutorial-data-sources.html#page-nested

And the nested objects works unless the scenario I posted in the previous fiddle.

Most of the operations won’t work for this type of data. However, as I can see here https://github.com/handsontable/handsontable/issues/6548 and here https://github.com/handsontable/handsontable/issues/3958 the changes for using nested objects might improve in version 8.0.0.

Using your code after losing pre 8.0.0-beta2 https://jsfiddle.net/9v7eysgf/ but still if we refer to ‘profile.name’ I guess that any object that does not contain ‘name’ should remove the error. I will investigate the subject, contact our Support Engineer and we’ll update you before the weekend.

Thanks so much @aleksandra_budnik

Hi @lijigang.us

If we use profile with null we get an error because we don’t have a name in profile object - https://jsfiddle.net/o94ghaeu/
So this is a reasonable effect based on how the JavaScript works.

On the other hand, if we omit profile we skip this error and thanks to that we can display data in the other cells with a good structure of data - https://jsfiddle.net/a2sLoxjh/

For deeper insight on how Handsontable works please look at our code from this line - https://github.com/handsontable/handsontable/blob/master/src/dataMap.js#L591

Hi @piotr.nowak

Thanks for the detailed explanation.

By looking at the code:

      for (let i = 0, ilen = sliced.length; i < ilen; i++) {
        out = out[sliced[i]];

        if (typeof out === 'undefined') {
          return null;
        }
      }

It already considers the case that profile is undefined which could mean the profile doesn’t exist on the data object. It makes sense.
I am just thinking if it also makes sense to consider that the value of profile could be null from design perspective.