-2

I am getting "ReferenceError: success is not defined" when doing a Restful call from controller to my node.js back end as follows:

authControllers.js:

authControllers.controller('authCtrl', ['$scope', '$location', '$window', 'UserService', 'AuthenticationService',
function authCtrl($scope, $location, $window, UserService, AuthenticationService) {
  $scope.me = function() {    
    UserService.me(function(res) {
      $scope.myDetails = res;
    }, function() {
      console.log('Failed to fetch details');
      $rootScope.error = 'Failed to fetch details';
    })
  };      
}]);

html:

<div class="row" data-ng-controller="authCtrl" data-ng-init="me()">
    <div class="col-lg-12">
        <div class="panel panel-primary">
            <div class="panel-heading">
                <strong>Your Details</strong>
            </div>
            <div class="panel-body">
                <p>{{myDetails.data.username}}</p>
                <p>{{myDetails.data.email}}</p>
            </div>
        </div>
    </div>
</div>

authServices.js:

authServices.factory('UserService',['$http', function($http) {
  return {        
    me:function() {
    return $http.get(options.api.base_url + '/me').success(success).error(error)
    }
  }
}]);

A successful nodeJs call is being received and it's returning data as well, but couldn't get it in the front end. Please help !

Narendra Solanki
  • 1,042
  • 14
  • 20

3 Answers3

2

I was starting a detailed explanation on PHP + Angular in order to solve that issue when I found that post detailing everything way better than me.

Please have a look : https://stackoverflow.com/a/39654288/3687474

aorfevre
  • 5,034
  • 3
  • 21
  • 51
1

You cannot connect to mongo directly from the angular application, you can use nodejs to connect to the mongodb and fetch data. Go through the following link,

NODE TODO LIST APP WITH MONGODB

Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
1

There's no way to access any DB from any framework that has been developed in javascript you need to have a server if you want to perform CURD operations first go through nodejs if you want server to be written in JS then you can go through rest calls in nodejs that will help you to develop your app.

Vikash Kumar
  • 1,712
  • 1
  • 11
  • 17