Array of structure passed from backend to frontend deserialized prematurely

Hi,

Suppose I have the following backend code

export function getData() {
 return [ { idx: 1}, {idx: 2}];
}

Then I called it from the frontend as follows

$w.onReady(function () {
    getData().then(data => {
        console.log(data);
    });
});

The data received at the frontend is array of two strings (instead of array of two structs) as follows:

Array(2)
0: "{\"idx\":1}"
1: "{\"idx\":2}"

Is it an intended behavior ? Or is it a bug ?

Thanks.

A conundrum… I just raised a note about whether I should expect a constructor of a function to de-serialize an object as a string as default behavior.

I have not been able to find if there is actually a specification for which side owns the behavior.

I just want it to work the way that suits me but there is something to be said for a standard.

Hey guys!

You can retrieve the data from the backend as a JSON (in this case - an array of objects) by adding JSON.stringify() .
Should look something like that:

export function getData() {
let arr = [ {idx:1}, {idx:2} ]
 return JSON.stringify(arr);
}

This is the output:

Hope it helps!

Doron.

Hi Doron,

Thank you for the answer.

I am still confused though, because I think Corvid internally perform the JSON serialization and deserialization when passing data between frontend ↔ backend. Is this behavior documented somewhere ?

It is also weird that when passing the same array from frontend to backend, I don’t need to call JSON.stringify.