skip to Main Content

I did try to call the api

export const users = {
list: () => http.get('users').then(r => r.data),

console.log( users.list()); and in the console I did get such result.
Promise {<pending>} [[Prototype]] : Promise [[PromiseState]] : "fulfilled" [[PromiseResult]] : Object data : (14) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] [[Prototype]] : Object
And I have question hot to get result the data from it like count of item or another from ‘Object data’ for example

2

Answers


  1. accessing the data property using dot notation

    users.list().then(response => {
      console.log(response.data); // get entire array of data
      console.log(response.data.length); // get count of items in the array
      // get specific item or property using array indexing or dot notation:
      console.log(response.data[0]); // get first item in the array
      console.log(response.data[0].name); // get name property of the first item
    }).catch(err => {
      console.log(err); // handle any errors during the api call
    });
    

    access the resolved value of the promise via .then(), the resolved value is the response object

    Login or Signup to reply.
  2. You can give a try like this:

    async fetchUsers() {
          try {
            const response = await users.list();
            this.userList = response;
          } catch (error) {
            console.error('Error fetching user data:', error);
          }
        },

    If you have users module then import it first at imports section!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search