I have this function that returns a result from a GET in an API (I am using an AWS Rest API, for contextualization):
const listUsers = async () => {
const apiName = 'AdminQueries'
const path = '/listUsers'
const params = {
headers: {
'Content-Type': 'application/json',
Authorization: `${(await Auth.currentSession())
.getAccessToken()
.getJwtToken()}`,
},
}
return await API.get(apiName, path, params)
}
And if i call the function to print on console, like:
console.log(listUsers())
I get an array on my console:
Promise {<pending>}
__proto__: Promise
[[PromiseState]]: "fulfilled"
[[PromiseResult]]: Object
Users: Array(42)
0: {Username: "00e534e3-08ee-4bc1-9d19-ab18208e3f94", Attributes: Array(5), UserCreateDate: "2021-09-06T10:05:35.537Z", UserLastModifiedDate: "2021-09-06T10:05:35.537Z", Enabled: true, …}
1: {Username: "05f4ce2e-4b57-48d5-9f5f-e18d250aefb4", Attributes: Array(6), UserCreateDate: "2021-09-10T19:45:32.002Z", UserLastModifiedDate: "2021-09-10T19:54:37.333Z", Enabled: true, …}
2: {Username: "06a78cc7-af6a-4fb6-975d-458c089b69a5", Attributes: Array(6), UserCreateDate: "2021-09-10T18:04:27.780Z", UserLastModifiedDate: "2021-09-10T18:04:27.780Z", Enabled: true, …}
3: {Username: "32652d77-e7a4-444a-9995-f8f50eb14633", Attributes: Array(6), UserCreateDate: "2021-09-06T15:48:24.897Z", UserLastModifiedDate: "2021-09-06T15:49:39.476Z", Enabled: true, …}
length: 42
__proto__: Array(0)
__proto__: Object
How do i map these users to show in an array? I tried but couldn't.
CodePudding user response:
Your listUsers function is returning a promise. To get the response you can make use of something like this
const listUsers = async () => {
const apiName = 'AdminQueries'
const path = '/listUsers'
const params = {
headers: {
'Content-Type': 'application/json',
Authorization: `${(await Auth.currentSession())
.getAccessToken()
.getJwtToken()}`,
},
}
return await API.get(apiName, path, params)
}
**** update ****
const func = async () => {
const response = await listUsers();
console.log(response);
}
func();
CodePudding user response:
const listUsers = async () => {
const apiName = 'AdminQueries'
const path = '/listUsers'
const params = {
headers: {
'Content-Type': 'application/json',
Authorization: `${(await Auth.currentSession())
.getAccessToken()
.getJwtToken()}`,
},
}
return await API.get(apiName, path, params)
}
const func = async () => {
const response = await listUsers();
reponse.data?.map((item) => {
console.log(item);
})
}