How can I "extract" and return my user array in getUsers?
Hi,
In the Metronic 8.1.0 version of React there is a User Management component.
In <code>src/app/modules/apps/user-management/users-list/core/_requests.ts</code> there's the <code>getUsers</code> method that fetches the users array:
<pre> const getUsers = (query: string): Promise<UsersQueryResponse> => { return axios .get(`${GET_USERS_URL}?${query}`) .then((d: AxiosResponse<UsersQueryResponse>) => d.data) } </pre><code>UsersQueryResponse</code> is declared in <code>src/app/modules/apps/user-management/users-list/core/_models.ts</code>:
<code>export type UsersQueryResponse = Response<Array<User>></code>
The actual data that gets returned looks like this:
<pre> [ { id: 1, name: Peter } { id: 2, name: Maria } { id: 3, name: John } ] </pre>Now, in my case, I get back a slightly different object from the server. The data object looks like this:
<pre> { list: [ { id: 1, name: Peter } { id: 2, name: Maria } { id: 3, name: John } ] } </pre>So as you can see the user array is one level further down in the hierarchy.
So my question is; how can I "extract" and return <em>my</em> user array in <code>getUsers</code>?
Replies (6)
Hi, I've finally had some time to test the code snippet you gave me.
This is what I did:
- Configured the types so that they now look like this:
- Now VSCode is complaining about the following line in <pre> src/app/modules/apps/user-management/users-list/core/QueryResponseProvider.tsx </pre> and in particular the <pre>response</pre> parameter:
The error message that I'm getting in VSCode is the following:
<pre> [{ "resource": "REDACTED/src/app/modules/apps/user-management/users-list/core/QueryResponseProvider.tsx", "owner": "typescript", "code": "2322", "severity": 8, "message": "Type 'UsersQueryResponse | undefined' is not assignable to type 'Response<User[]> | undefined'.\n Type 'UsersQueryResponse' is not assignable to type 'Response<User[]>'.\n Types of property 'data' are incompatible.\n Type 'CustomType | undefined' is not assignable to type 'User[] | undefined'.\n Type 'CustomType' is missing the following properties from type 'User[]': length, pop, push, concat, and 35 more.", "source": "ts", "startLineNumber": 52, "startColumn": 76, "endLineNumber": 52, "endColumn": 84, "relatedInformation": [ { "startLineNumber": 53, "startColumn": 3, "endLineNumber": 53, "endColumn": 11, "message": "The expected type comes from property 'response' which is declared here on type 'QueryResponseContextProps<User>'", "resource": "REDACTED/src/_metronic/helpers/crud-helper/models.ts" } ] }] </pre>Any ideas why it is giving me this error and how to correct it?
Thanks.
Hi,
Did you update types in src/app/modules/apps/user-management/users-list/core/_requests.ts?
Regards,
Lauris Stepanovs,
Keenthemes Support Team
Hi,
Please don't hesitate to reach out if you need anything more from us.
Regards,
Lauris Stepanovs,
Keenthemes Support Team
Your Support Matters!
Hi,
Thank you for reaching out to us.
The response is a generic type where you can specify your data response, if you have a different data structure instead of passing a user array type you can pass a custom type.
Check out the example in this gist file.
Regards,
Lauris Stepanovs,
Keenthemes Support Team