Question

Why does this return a 400 error (Unique query may have at most one result. Got 100):

[{
    name:null,
    type:'music/artist'
}]

I'd expect it to return the first 100 names from music/artist? How is it a unique query?

I'm trying to build a random query, where I can provide two params - the page and the result from within the page to return - and get one item back

Would be awesome if MQL supported something like return: 'random'

EDIT - adds full Angular factory. The query runs fine if I punch it in to the freebase.com/query engine.

myApp.factory('FB_Random', function ($http) {
    return {
        fn: function (callback) {
            $http.get("https://www.googleapis.com/freebase/v1/mqlread",
                {
                    params: {
                        query:[{
                            name: null,
                            type: '/music/artist'
                        }]                        
                    }
                }
            ).success(function (d, status, headers, config) {
                callback(d.result);
            }).error(function (d, status, headers, config) {
                console.log(d);
            });
        }
    };
});
Était-ce utile?

La solution

Looks like there's some undocumented Angular behavior going on here. According to the Angular docs, the params parameter takes a "Map of strings or objects which will be turned to ?key1=value1&key2=value2 after the url."

However, it seems like there's some additional logic that looks for array values in the params map and turns them into repeated parameters; ie. ?query={...}&query={...}. Since your query is (properly) wrapped in array notation, indicating that you're expecting multiple results, Angular interprets it as a list with a single value for the query parameter. It then extracts that query parameter and adds it to the url like so:

https://www.googleapis.com/freebase/v1/mqlread?query={%22name%22:null,%22type%22:%22/music/artist%22}

This is why you get the error about your query incorrectly asking for a single result. Because Angular removed the outer array notation that's needed by MQL.

The simplest way to work around this is to simply wrap your query in an additional array like this:

$http.get("https://www.googleapis.com/freebase/v1/mqlread",
  {
    params: {
      query:[[{
        name: null,
        type: '/music/artist'
      }]]                       
    }
  }
)

If you'd like to make the fix more explicit and your code easier to read you can just stringify the MQL query yourself like this:

$http.get("https://www.googleapis.com/freebase/v1/mqlread",
  {
    params: {
      query: JSON.stringify([{
        name: null,
        type: '/music/artist'
      }])                     
    }
  }
)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top