Aaron
Aaron

Reputation: 4480

Vue.js calling an async function from external js file

I am trying to create a .js file where I have a couple of my async calls. I set up the file, but am not getting any results when I call my method. This is all new to me to call from a .js file, so not sure what I am doing wrong.

Here is my inventory.js fileimport axios from "axios";

let getInventories = async () => {
  const result = await axios
    .get("/inventories")
    .catch((error) => console.log(error));
  // this.inventoryArray = result.data;
}

export {getInventories}

Here is the call from my Inventory.vue file

import axios from "axios";
import { bus } from "../app";
import {getInventories} from './inventory';
export default {
  mounted() {
    let temp =  getInventories();
    debugger;
  },
}

temp not returning anything. I add await in from of getInventories but get an error

Upvotes: 0

Views: 1091

Answers (1)

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You're missing to return the result :

let getInventories = async () => {
   try{
    const result = await axios
    .get("/inventories")
    return result.data;
   }   catch(error){
   console.log(error);
   return null;
   };
 
}

export {getInventories}

Upvotes: 1

Related Questions