Sam99
Sam99

Reputation: 25

Axios api call, export variable from inside a function

I have this function in request.js

import Axios from "axios"
export async function get(res, req, next) {
    var resp = await Axios.get("https://api.pro.coinbase.com/products");
    let pairs = resp.data.map(a => a.id);
    console.log(pairs)
}

in my component I have this :

<script>

        import { get } from "../data/request.js";
    
        function getSomething() {
            get();
        }

</script>

<h1>get something here</h1>
<button on:click={getSomething}>Get !</button>

This way I manage to get the pairs in the console but I use the whole function, how can I use the variable "pairs" on its own in my component ?

Thank you

Upvotes: 1

Views: 322

Answers (1)

Hitech Hitesh
Hitech Hitesh

Reputation: 1635

import Axios from "axios"
export async function get(res, req, next) {
    var resp = await Axios.get("https://api.pro.coinbase.com/products");
    let pairs = resp.data.map(a => a.id);
    console.log(pairs)
return pairs;
}

Add the return keyword

<script>

        import { get } from "../data/request.js";
    let pairs
        function getSomething() {
        pairs=   get();
      console.log(pairs)
        }

</script>

<h1>get something here</h1>
<button on:click={getSomething}>Get !</button>

Try this

Upvotes: 1

Related Questions