skip to Main Content

I have this:

file accounts.controlles.ts

import { requestT } from "src/service/request.api";

export const getaccounts = async () => {
    const response = await requestT({
        method: "GET",
        url: "/accounts",
    }); 
    return response;
};

file header.tsx

import { getaccounts } from "src/controllers/accounts.controller";

export default function Header() {

    const listaccounts =  async () => { return await getaccounts(); };
    console.log("listaccounts: ", listaccounts);

}

but I got this on console:

listaccounts:  async ()=>{
        return (0,src_controllers_accounts_controller__WEBPACK_IMPORTED_MODULE_7__.getaccounts)();
    }

I just want get the items of service getaccounts , why I’m getting the arrow function???…..

I test the function getaccounts, I added console.log(response) and I got the items, what can I do to get it on header.tsx??

2

Answers


  1. listaccounts as you’ve defined it is simply an async function that calls another async function. You are printing the function definition. You never called the actual function.

    Try calling the function, awaiting its return, then logging the result. Note that to call an async function this way, the caller must also be async.

    export default async function Header() {
    
        const listaccounts =  async () => { return await getaccounts(); };
        const accounts = await listaccounts();
        console.log("listaccounts: ", accounts);
    
    }
    

    If you don’t want to make Header async, then you can simply provide the callback behavior to print the accounts after they are available. The .then() method works well for this.

    export default function Header() {
    
        const listaccounts =  async () => { return await getaccounts(); };
        listaccounts().then(accounts => {
            console.log("accounts: ", accounts);
        });
    }
    
    Login or Signup to reply.
  2. That’s exactly what you said to JS to print.

    You need to invoke the function in the console.log, if you’re expecting for something else.

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