1
votes

github code: https://github.com/aurora10/amazone-clone.git

I try to utilize Axios to hit an API but get an error:

The error in console is NUXT SSR:

TypeError: Cannot read property '$get' of undefined
at asyncData (pages/index.js:98:35)
at promisify (server.js:1898:15)
at Promise.all.Components.map.Component (server.js:1573:82)
at Array.map (<anonymous>)
at module.exports../.nuxt/server.js.__webpack_exports__.default (server.js:1569:51)

This is how I try to do it:

export default {

async asyncData({ $axios }) {
  try {
    let response = await $axios.$get("http://localhost:3000/api/products");
    console.log(response);
    return {
      products: response.products
    };
  }catch (err) {

    console.log(err);

  }
}

}

The API itself works. If I call it from browser - it gives me the list of products. What am I doing wrong?

Complete fille:

<template>

<div class="container-fluid browsing-history">
  <div class="row">


    <div class="col-sm-8 col-8">

      <h1 class="a-size-large a-spacing-none a-text-normal">All products</h1>
      <div class="a-spacing-large"></div>

      <a href="#" class="a-button-buy-again">Add new product</a>
      <a href="#" class="a-button-history margin-right-10">Add new category</a>
      <a href="#" class="a-button-buy-again margin-right-10">Add new owner</a>
    </div>
  </div>
</div>
<div class="a-spacing-large"></div>

<div class="container-fluid browsing-history">
  <div class="row">
    <div class="col-xl-2 col-lg-2 col-md-3 col-sm-6 col-6 br bb">

      <div class="history-box">
        <!--          product page-->
        <a href="#" class="a-link-normal">
          <img src="" class="img-fluid" alt="">
        </a>

        <div class="a-spacing-top-base asin-title">
        <span class="a-text-normal">
          <div class="p13n-sc-truncated">Title </div>
        </span>
        </div>

        <div class="a-row">
          <a href="#">
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
            <i class="fas fa-star"></i>
          </a>
          <span class="a-letter-space"></span>
          <span class="a-color-tertiary a-size-small asin-reviews">(1732)</span>
        </div>
      </div>
      <!--price-->
      <div class="a-row">
        <span class="p13-sc-price">$25</span>
      </div>
      <!--byttons-->
      <div class="a-row">
        <a href="#" class="a-button-history margin-right-10">Update</a>
        <a href="#" class="a-button-history margin-right-10">Delete</a>
      </div>
    </div>
  </div>
</div>

export default { async asyncData({$axios}) { try { let response = await $axios.$get("http://localhost:3000/api/products"); console.log(response); return { products: products }; } catch (err) { console.log(err); } } };

I did make sure it is installed. I had also included

module.exports = {
  modules: [
    '@nuxtjs/axios',
  ],

  axios: {
    // proxyHeaders: false
  }
}

just like they tell in the the installation manual. Also tried to remove $ before get...still the same error in console:( I have no clue what else could be wrong..

2
Where is asyncData being called? You may be calling it multiple times by accident and it's passing a undefined valueHides
It is being called only in in index.vue file. The only place I used it. see above.PinPiguin
Could you try adding the file again? It's a bit hard to readHides
this website does not parse the code properly. I comes so messy:( pastebin.pl/view/e944f92fPinPiguin
Could you provide your nuxt.config.js file?Hides

2 Answers

2
votes

It looks like you are not including the axios module, look at the installation process here https://axios.nuxtjs.org/setup.html#install

Your config should then look like this in your export default

  /*
  ** Nuxt.js modules
  */
  modules: [
    // Doc: https://bootstrap-vue.js.org
    'bootstrap-vue/nuxt',
    '@nuxtjs/axios'
  ],

  axios: {
    // proxyHeaders: false
  }
0
votes

Here are the steps for axios in Nuxt

  1. Make sure axios is installed npm install @nuxtjs/axios
  2. Add axios in nuxt.config.js
...
  axios: {
    // proxyHeaders: false
  }
...
  1. You don't need the $ in $axios.$get. It should be $axios.get()