-1

I have this function on a React component, that fetches data from a AWS API and returns it on an Object.

I tried transforming the data received in the object in an array.

The function that fetches the data is listUsers.

Then i used the push method. But it doesn't matter what i do, if i log the typeof either from data or array it continues returning an object.

Here's the full code:

import { Auth, API } from 'aws-amplify'
import { useState, useEffect } from 'react'

export default function Users() {
    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 usersArray = async () => {
    const response = await listUsers()

    var array = []

    for (const item of response.Users) {
      array.push(item)
    }

    setData(array)
    console.log(array)
    console.log(typeof array)
  }

  useEffect(() => usersArray(), [])

  return <></>
}

And the thing is that i NEED IT to become an array, cause i need to use Map to make the component render the data the way i want.

Any tips?

Henrique Monteiro
  • 107
  • 1
  • 1
  • 9

2 Answers2

0

Nearly everything in JavaScript is an object.

Only — null , undefined , strings, numbers, boolean, and symbols are not objects.

kup
  • 731
  • 5
  • 18
0

You are checking it wrong. You should use array instance of Array to confirm if it is an array type or not.

Hariom Balhara
  • 832
  • 8
  • 19