API requests does not work, no error shown(svelte, sapper)

222 views Asked by At

I am trying to use the api of a website and I don't understand why my code does not work. It is supposed to show the api in the console at the end as shown in this video (https://www.youtube.com/watch?v=ujbE0mzX-CU) starting at 1h58mn55s (result around 2h11mn45). I did everything like it is shown, but for some reason my console shows nothing, and I can't figure out where the problem comes from (I don't have any error). my files are as follow :

requests.js

import axios from 'axios';
import parsers from './parsers';

async function coinbaseMarkets() {
    const response = await axios.get('https://api.pro.coinbase.com/products');
    return parsers.coinbaseMarkets(response.data);
}

export default {
    coinbaseMarkets,
};

parsers.js

function coinbaseMarkets(data) {
    const [cbMarketPairs] = data;

    return {
        pairs: cbMarketPairs.id,
    }
}

export default {
    coinbaseMarkets,
};

component.svelte(he uses a route file and not a component file but I tried both and neither worked)

<script context="module">

  import requests from '../data/requests.js';
  
  export async function preload() {
    try {
      const coinbaseMarkets = await requests.coinbaseMarkets();

      return {coinbaseMarkets};
    } catch(e) {

    }

  }
</script>
<script>
 export let coinbaseMarkets;
 console.log(coinbaseMarkets, "coinbaseMarkets");
</script>

Thanks for your help and your time

2

There are 2 answers

0
James Chen On

Thanks for the replies, I found the solution I can't work in a component it has to be called in a route file.

0
Oleg Gulevskyy On

I have not had a luck using Svelte yet, but I am not sure why do you have the same function twice?

function coinbaseMarkets()

Also, what are the results of console logging response and parsers.coinbaseMarkets(response.data);