Need help with angularjs SDK


#1

I have the following error.

TypeError: Cannot read property 'login' of undefined
    at new <anonymous> (controllers.js:136)
    at invoke (ionic.bundle.js:11591)
    at Object.instantiate (ionic.bundle.js:11602)
    at $get (ionic.bundle.js:14906)
    at updateView (ionic.bundle.js:42986)
    at IonicModule.directive.directive.compile.eventHook (ionic.bundle.js:42933)
    at Scope.$get.Scope.$broadcast (ionic.bundle.js:20605)
    at $state.transitionTo.$state.transition.resolved.then.$state.transition (angular-ui-router.js:2114)
    at deferred.promise.then.wrappedCallback (ionic.bundle.js:19197)
    at ionic.bundle.js:19283

Here is a snippet from my code.

/* app.js */

angular
  .module('myapp', ['ionic',
    'myapp.controllers'
    'dfUserManagement',
    'ngDreamFactory',
  ])

/* controllers.js */

angular.module('myapp.controllers', [])

    .controller('MainCtrl', function($scope, $ionicModal, $timeout, $location, DreamFactory) {

        // model for login credentials
        $scope.creds = {
            "body": {
              "email": 'mydspadminemail',
              "password": 'mydspadminpassword'
            }
        }

        DreamFactory.api.user.login($scope.creds,

            // Success function
            function(result) {
                console.log(result)
            },

            // Error function
            function(error) {

            });

    })

/* index header */

<!-- ionic/angularjs js -->
<script src="lib/ionic/js/ionic.bundle.js"></script>

<!-- cordova script (this will be a 404 during development) -->
<script src="cordova.js"></script>

<!-- Dream Factory -->
<script type="text/javascript" src="lib/dreamfactory-user-management/dreamfactory-user-management.js"></script>
<script type="text/javascript" src="lib/angular-dreamfactory/angular-dreamfactory.js"></script>

I get a 200 status on all services. user, system, db etc, and all of them say they cant find a valid session.

Now, I have a plugin for dreamfactory-user management that I got from a DF blog post, along side this. i’d like to use that instead of the sdk login if possible.

I get the same issue when accessing DreamFactory.api.[myservice].getRecords().


#2

First, You have to wait until api is ready if You want to access api. Also You should add DF header to Your calls. Take a look at this:

var MyApp= angular.module("MyApp", ["ngResource", "ngRoute", "ngDreamFactory"])
    .constant('DSP_URL', 'https://YOURDSP.cloud.dreamfactory.com')
    .constant('DSP_API_KEY', 'YOURAPPNAME')
    .constant('DEV_ENV', 0)
    .config(function ($routeProvider) {
        "use strict";
        $routeProvider
            .when('/', { 
                controller: 'MainCtrl',
                templateUrl: 'indexl.html' 
            });
    })
   .config(['$httpProvider', 'DSP_API_KEY', function($httpProvider, DSP_API_KEY) {
       $httpProvider.defaults.headers.common['X-DreamFactory-Application-Name'] = DSP_API_KEY;
   }]);
    
MyApp.controller('MainCtrl',['$scope', 'DreamFactory', function ($scope, DreamFactory) {
    "use strict";
    
    $scope.login = function(){
        var creds = {
                email: "EMAILOFUSERWHOCANACCESAPI",
                password: "PSSWORDOFTHISUSER"
            };
        var postData = {
                    body: creds
                };
                
        DreamFactory.api.user.login(postData,
            function (data) {
                console.log('LoggedIn');
            },
            function (errors) {
                throw {message: errors.error[0].message}
            });
    }
    
    $scope.checkAPI = function(){
        console.log(DreamFactory.isReady());
    }

    //Gets called when API connects to Database
    $scope.$on('api:ready',function(){
        //Call Your functions that access API ONLY after this event occurs or if DreamFactory.isReady() is true
    });

}]);