How does kotlinJS treat a mutableList

I have been fetching data from my api and casting it to a mutableList() of my data class. However it seems not to work as expected. But when i cast my response to arrayOf() of my data class everything works out fine.

Can you give a short code example? What do you mean by casting to mutableList(). Do you mean MutableList and Array instead of mutableList() and arrayOf? The later ones are functions and not classes.

What do you expect to happen and what actually happens?

1 Like

// The code below returns [Object object]

return axios<MutableList<MyDataClass>>(jsObject {
this.url = url
data = body
this.method = method
headers = mapOf(“Content-Type” to “application/json”,
“Accept” to “application/json”, “Authorization” to “Bearer $token”)
}).await().data

// The code below returns an Array of objects

return axios<Array<MyDataClass>>(jsObject {
this.url = url
data = body
this.method = method
headers = mapOf(“Content-Type” to “application/json”,
“Accept” to “application/json”, “Authorization” to “Bearer $token”)
}).await().data

I expected the first code to return a list of objects as the second code does return an Array of objects.
However when i print the value of the first code using JSON.stringify(the response [Object object]), i can see the objects wrapped in an array like [{the data}, {the data}]

The API that you are using does probably not convert the data according to the type parameter. The type parameter needs to adhere to the data’s real type.

I’d suggest to retrieve the array and convert it to a MutableList yourself. There are plenty of ways to convert an array to MutableList in Kotlin.

1 Like

Thanks @fatjoe79 and @Wasabi375 for your responses. Will try that.

@fatjoe79 i retrieved the array and converted it to a MutableList and it worked fine. Thanks once again. :slightly_smiling_face: