Billal BEGUERADJ
Billal BEGUERADJ

Reputation: 22804

Nuxt.js: ReferenceError baseURL is not defined

I have a Nuxt.js app. I want to read a local JSON file located in the static/ folder.

I have in my nuxt.config.js file:

module.exports = {                                                                                                                                                       
    modules: [                                                                                                                                                           
        ['@nuxtjs/axios', {                                                                                                                                              
            baseURL: 'http://localhost:3000',                                                                                                                            
            browserBaseURL: 'http://localhost:3000'                                                                                                                      

        }]                                                                                                                                                               
    ],                                                                                                                                                                   

    axios: {                                                                                                                                                             
        // proxyHeaders: false                                                                                                                                           
        baseURL: "http://localhost:3000/"                                                                                                                                
    },        

In my pages/index.vue:

<template>                                                                                                                                                          
<section class="container">                                                                                                                                  
  <p>{{ data }}</p>                                                                                                                                                
</section>                                                                                                                                                               
</template>                                                                                                                                                              

<script>                                                                                                                      
import axios from 'axios'                                                                                                                                                

export default {                                                                                                                                                         
    components: {                                                                                                                                                        
        Make                                                                                                                                                             
    },                                                                                                                                                                   
     async asyncData({ app }) {                                                                                                                                        
      const data = await app.$axios.$get(baseURL + 'static/db.json')                                                                                                         
      return { data }                                                                                                                                                    
     }                                                                                                                                                                 

}       
</script>

I am getting this error:

 ReferenceError
 baseURL is not defined

Screenshot:

enter image description here

When I hard code the URL:

async asyncData({ app }) {                                    
        const data = await app.$axios.$get('http://localhost:3000/static/db.json')                                                                                     
        return { data }                                                                                                                                                    
    }    

I get this error message instead:

NuxtServerError
Request failed with status code 404

Any help?

Reference: https://axios.nuxtjs.org/

Upvotes: 2

Views: 3868

Answers (1)

Aldarund
Aldarund

Reputation: 17621

You don't need to add baseURL into axios call if you set it into module options. It will be prepended automatically.

And thats not how it work. If u define something in config it dont appear magically in your code. Anything you reference should be defined or imported.

Upvotes: 1

Related Questions