Md. Nayem
Md. Nayem

Reputation: 41

Vue warn: Error in created hook: "TypeError: Cannot read property 'get' of undefined"

When I use axios I got this error:

[Vue warn]: Error in created hook: "TypeError: Cannot read property 'get' of undefined"

export default {
        methods: {
            loadUsers(){
                axios.get("api/user").then(data  => (this.users = data));
            }
        },
        created() {
            this.loadUsers();
        }
    }

Routes: api.php

Route::apiResources(['user' => 'API\UserController']);

Controller: API/UserController.php

public function index()
    {
        return User::latest()->paginate(5);
    }

Upvotes: 4

Views: 8634

Answers (2)

ajit deshmukh
ajit deshmukh

Reputation: 131

I was getting same error .this error comes due to the vue cannot identify the get property which is belongs to the 'vue-resource' package so my recommendation is need to import package it will help to resolse this issue follow below step

first check 'vue-resource' installed on your project otherwise install it

npm install vue-resource

In main.js file include this

import Vue from 'vue' import VueResource from 'vue-resource' Vue.use(VueResource);

Upvotes: 0

Mike Harrison
Mike Harrison

Reputation: 1393

You need to import Axios first:

import axios from 'axios'
export default { 
    // ... axios.get will work now
}

Upvotes: 3

Related Questions