Андрей Гузюк Андрей Гузюк - 4 months ago 9
Node.js Question

my $http.get in angular doesn't work

i have problem with $http.get when trying to get data from my db ( mongoDB )
I'm using mean stack .

angular.module('wildroseApp')
.controller('WildroseComponent', function($scope, $http){
$http.get('/api/wildrose')
.success(function (data) {
$scope.wildrose = data;
console.log($scope.wildrose);
})
.error(function (err) {
alert('Error!');
});
})
.component('wildrose', {
templateUrl: 'app/wildrose/wildrose.html',
controller: WildroseComponent,
controllerAs: Wildrose
});


and I have one more question yet;
When I'm using "controllerAs" in component my browser says


Uncaught ReferenceError: Wildrose is not defined


but if remove it all is ok

I solve it
just added in constructor a little bit code

class WildroseComponent {
constructor($http) {
this.$http = $http;
this.wildrose = [];
}
$onInit(){
this.$http.get('/api/wildrose')
.then(response => {
this.wildrose = response.data;
console.log(this.wildrose);
});;
}
}

Answer

ControllerAs requires string and not an object:

controllerAs: "wildrose"

Have you set up an endpoint at your server with node/express under /api/wildrose that returns a json string read from your database?

Please provide more code from your back-end and errors, otherwise it is hard to diagnose.