Serverconnect formatter not working on api-returned data

Wappler Version : 2.5.5
Operating System : Mac OSX Mojave v10.14.5
groupBy.php.zip (1.3 KB)

Hi all,

I have attached the server action file (uses public api sample data) for the below bug report.

Details

image

  1. I have an public api that returns a dataset which is an array of objects

  2. The api data is assigned to variable array_of_objects1, output is printed

  3. The api data is also put through a repeat and the two properties of each object is printed

The output of both steps 2 & 3 are exactly the same:

[{
	"name": "dept1",
	"staff": "john"
}, {
	"name": "dept1",
	"staff": "jack"
}, {
	"name": "dept2",
	"staff": "joe"
}, {
	"name": "dept3",
	"staff": "jim"
}]

.
In the last two steps, I tried using the groupBy formatter on the above two “array of objects”:

  1. When the groupBy is tried on the variable array_of_objects1 that holds the api data, I’m getting the following error:

  2. When the groupBy is tried on the repeat array_of_objects2 that outputs the same data, the group by seems to work just fine. The output prints correctly as ["dept1","dept2","dept3"]

It’s exactly the same data, while the groupBy formatter works on a repeat output (step-3, which is my current workaround), its not working directly on api returned data (expected & ideal).

Also, not sure if this issue is only with the groupBy formatter not working on api-returned “array of objects”.

I also tried using the back-ticks in the expression & got the following error.

“message”:“Unexpected character ` at column 26 in expression {{array_of_objects1.groupBy(`name`).keys()}}”

Any luck looking into this bug?

If not a bug, I just want to know if I have missed something. Thanks in advance.

@patrick, are you able to advise on this bug please. Thanks in advance!

Test if this update solves the problem, replace the file dmxConnectLib/lib/formatters/collections.php.

collections.zip (1002 Bytes)

Hi @patrick, no it didn’t work, throwing the following error:

“code”:0,“file”:"/Users/akayy/Wappler/myapp/myapp_alpha/dmxConnectLib/lib/formatters/collections.php",“line”:72,“message”:“syntax error, unexpected ‘(array)’ (array) (T_ARRAY_CAST)”,“trace”:"#0

I’m attaching the test server-action file (uses a public api) used in my original bug report. Clicking on “Open in Browser” should open the output of the file. groupBy.php.zip (1.3 KB)

image

Problem is that PHP doesn’t really have objects, sometimes they are created as keyed arrays and sometimes they derive from the stdClass. An array is accessed as $item[$prop] and a class $item->$prop. We normally use arrays internally in Server Connect, but the API seems to have returned a class instead. In the file I tried to case item as file in the foreach ($val as (array)$item), this seems not to be allowed there. So we could try to put the cast on the next line as $item = (array)$item;.

Full code for the function becomes:

function formatter_groupBy($val, $prop) {
    if (!is_array($val)) return NULL;
    $groups = array();
    foreach ($val as $item) {
        $item = (array)$item;
        $key = strval($item[$prop]);

        if (!array_key_exists($key, $groups)) {
            $groups[$key] = array();
        }

        $groups[$key][] = $item;
    }
    return (object)$groups;
}
1 Like

Thanks a ton @patrick, this worked :slight_smile: Also, thanks for the extra write-up, great learning!