[Vuejs]-Vue.js method PUT working but method GET not in MongoDB

0👍

Check your method listCabs. You are providing a function where you reference this, but this is no longer what you think it is inside the callback function. Because you are using an arrow function this should refer to the gobal Window object in this context.

If you had used a normal function declaration instead of a arrow-function, this would refer to the function and also not the vm.
You should do some research on this topic to fully understand why this is.

Anyway the solution is to create a new Variable binding to the vm (aka closure) before starting to fetch. And then refer to the ViewModel using the newly created vm variable.

Your code should work with this update:

listCabs(){   
    const vm = this
    return fetch('api/cabs') 
    .then(res => res.json())
    .then(data => {
        vm.cabs = data;
        console.log(vm.cabs);      
   })            
}

Leave a comment