Any Restangular Example?

Hi There

Does anyone has used DSP api with restangular . I am getting a return promise with Array[0] object. I believe I do not have the

$httpProvider.defaults.headers.common['X-DreamFactory-Application-Name'] = DSP_API_KEY; 

in restangular

I have used this code but still nothing

RestangularProvider.setDefaultHeaders({ 'X-DreamFactory-Application-Name': DSP_API_KEY });

I have, if you are following this example for instance:

Stick this in your app.js file [Assuming you have added the required dependencies too]:

.config(['RestangularProvider', 'DSP_URL', function(RestangularProvider, DSP_URL) {
	RestangularProvider.setBaseUrl(DSP_URL);
}])

You can then use restangular in your controllers with something like this:

  .controller('someController', ['$scope', 'Restangular',
  function($scope, Restangular) {
  	
	var resource = Restangular.all('rest/your_db');

        resource.get(record_id).then(function(response){
			console.log(response);
	});

    });

You don’t need to set the headers using this method.

2 Likes

Thanks Antony

My code looks similar to your code however, I am using it as a factory not controller

.factory('Links', [ 'Restangular' , function (Restangular) {
  
     var mainLinks2 = Restangular.all('admin_dashlinks').getList().then(function (response){
         
         console.log(response);
     });

The error I am getting is

TypeError: Cannot read property 'data' of undefined
    at index.js:22
    at restangular.js:298
    at Function.forEach (lodash.compat.js:3640)
    at Object.Configurer.init.config.responseExtractor (restangular.js:297)
    at parseResponse (restangular.js:1033)
    at restangular.js:1065
    at processQueue (angular.js:13292)
    at angular.js:13308
    at Scope.$get.Scope.$eval (angular.js:14547)
    at Scope.$get.Scope.$digest (angular.js:14363)

I’ve not used it in a factory before, if you’ve not seen this it may be of use:

Thanks Antony

I have used it too but in vain so what I did was used http and this is working perfect;

   .constant('DSP_URL', 'MY_URL')
    .constant('DSP_API_KEY', 'MY_API_KEY')
    .config(['$httpProvider', 'DSP_API_KEY', function ($httpProvider, DSP_API_KEY) {

    // Set default headers for http requests
    $httpProvider.defaults.headers.common['X-DreamFactory-Application-Name'] = DSP_API_KEY;
}])

    .run(['$cookieStore', '$http', 'UserDataService', function ($cookieStore, $http, UserDataService) {

    var cookie = $cookieStore.get('CurrentUserObj');

    // Check if there is a CurrentUserObj in the cookie
    if (cookie) {

        // There is so store it for a sec
        UserDataService.setCurrentUser($cookieStore.get('CurrentUserObj'));

        $http.defaults.headers.common['X-DreamFactory-Session-Token'] = cookie.session_id;
    }
}])

However, the equivalent code in Restangular is giving the error still.

*Note: I am testing both setDefaultHeaders and setDefaultHttpFields

config([ 'RestangularProvider', 'DSP_API_KEY', 'DSP_URL' ,function(RestangularProvider, DSP_API_KEY, DSP_URL) {
RestangularProvider.setBaseUrl( DSP_URL+'/rest/rad/');
RestangularProvider.setDefaultHeaders({ 'X-DreamFactory-Application-Name': DSP_API_KEY ,'Access-Control-Allow-Credentials': true });
RestangularProvider.setDefaultHttpFields({ 'X-DreamFactory-Application-Name': DSP_API_KEY ,'Access-Control-Allow-Credentials': true });
    // Restangular Config

      RestangularProvider.addResponseInterceptor(function(data, operation, what, url, response, deferred) {
      var extractedData;
      // .. to look for getList operations
      if (operation === "getList") {
        // .. and handle the data and meta data
        extractedData = data.data.data;
        extractedData.meta = data.data.meta;
      } else {
        extractedData = data.data;
      }
      return extractedData;
    });


}]) 

  .run(['$cookieStore', 'Restangular', 'UserDataService', function ($cookieStore, Restangular, UserDataService) {

// Let us know what the module is up to
//console.log('RUN BLOCK: Check for and set current user');
var cookie = $cookieStore.get('CurrentUserObj');

// Check if there is a CurrentUserObj in the cookie
if (cookie) {

    // There is so store it for a sec
    UserDataService.setCurrentUser($cookieStore.get('CurrentUserObj'));
    Restangular.setDefaultHeaders({ 'X-DreamFactory-Session-Token': cookie.session_id });
    Restangular.setDefaultHttpFields({ 'X-DreamFactory-Session-Token': cookie.session_id });

}
}])

I am sure I will find the solution one day but for now I will revert to old firend $http