0

Below is my AirTableService.js

(function () {
    "use strict";   
    var AirTableService = function ($http, $q) {
        var AirTableMethods = {
            getMyRounds: function(AirTable_secret){
                var deferObject_myRounds;
                var myRounds_promise = $http.get('https://api.airtable.com/v0/XXXXXXX/Rounds?view=Main%20View&maxRecords=10&callback=JSON_CALLBACK', {
                    headers : {
                        'Authorization' : AirTable_secret.apikey,
                        'Content-Type' : 'application/json'
                    }
                });
                deferObject_myRounds = deferObject_myRounds || $q.defer();

                myRounds_promise.then(function(data){
                    deferObject_myRounds.resolve(data);
                });                
                return deferObject_myRounds.promise;
            }
        };
        return AirTableMethods;
    };

    AirTableService.$inject = ['$http', '$q'];

    angular.module('appGolf')
      .service('AirTableService', AirTableService);

}());

In there as you can see, using AirTable's api I am trying to GET data from my table. I'm passing the parameters view and maxRecords and it works. Documentation states I can pass sort,

enter image description here

which I then changed to,

https://api.airtable.com/v0/XXXXXXX/Rounds?view=Main%20View&maxRecords=10&sort=[{field:'RoundID',direction:'desc'}]&callback=JSON_CALLBACK

and clearly that doesn't work and it it gives me this error, enter image description here
I know this is because sort is a array of objects and I know how I am passing this is incorrect.

My question is, how do you do this in AngularJS?

Thank you in advance.

5
  • Possible duplicate of Send array via GET request with AngularJS' $http service Commented May 3, 2016 at 14:14
  • have you tried double quotes like the example shows? Commented May 3, 2016 at 14:18
  • $http already returns a promise. You don't have to use $q. Commented May 3, 2016 at 14:20
  • @DrewJordan I just tried, no difference, same error Commented May 3, 2016 at 14:25
  • your error message shows an extra single quote after 'desc', you didn't add that by mistake, right? Commented May 3, 2016 at 14:30

2 Answers 2

2

Found the answer here As mentioned there, I needed to add,

paramSerializer: '$httpParamSerializerJQLike',

And if you are interested, my function now looks like,

var myRounds_promise = $http.get('https://api.airtable.com/v0/XXXXX/Rounds?callback=JSON_CALLBACK', {
                params: {
                    view: 'Main View',       
                    maxRecords: 10,
                    sort: [{"field": 'RoundID', "direction":'desc'}]                        
                },
                paramSerializer: '$httpParamSerializerJQLike',                    
                headers : {
                    'Authorization' : AirTable_secret.apikey,
                    'Content-Type' : 'application/json'
                }
            });

Thanks everyone for their suggestions and helping me out.

Sign up to request clarification or add additional context in comments.

Comments

1

Your service is very verbose and hard to read. I would write it like this:

var app = angular.module("myApp", [ /* dependencies */ ]);

app.factory("AirTableService", ["$http", function($http) {
    return {
        getMyRounds: function(AirTable_secret) {
            return $http.get('path/to/API', {
                //put your sorting JSON object here in params
                params: { sort: [{field: "RoundID", direction: "desc"}] },
                headers: {
                    'Authorization' : AirTable_secret.apikey,
                    'Content-Type' : 'application/json'
                }
            });
        },
        anotherMethod: function() {
            //etc....
        },
        yetAnotherMethod: function() {
            return $http.post(); //maybe POST something
        }
    };
}]);

Inject it to your controller and use:

AirTableService.getMyRounds(airtableSecret).then(successCallback).catch(errorCallback);

1 Comment

I tried as you suggested but still getting the same error. BTW appreciate formatting my service, it makes sense.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.