Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

JavaScript

Function doesn't return data to $scope variable. - AngularJS

Hello,

const app = angular.module("myApp", []);

app.controller("myCtrl", function($scope) {     
    function getData() {
        axios.get(`http://localhost:3001/api/movies`)
            .then(response => {
            console.log(response.data); // shows the data
            return response.data;
        })
        .catch(e => console.error(e.message));
    }

    console.log(getData()); // undefined
    $scope.movies = getData();
});

Console logging response.data gives back my data. But if I try and store the return in a $scope.movies variable. it's undefined...

Any clue?

Thank you?