I’m trying to connect to the Mopidy API to play spotify and I’m using Javascript. When I try to execute the following code, I get some errors:
function play(currentTrack){
var track = 'spotify:track:2NtFcdHF5rtDkWyNb5Up8v'
mopidy.tracklist.add("uri",track).then(function(data){
});
How should I structure this? I’ve tried listing all the params and adding ‘null’ to them, I’ve tried just using components of the URI (which is actually a result of a different call) such as just the “2NtFcdHF5rtDkWyNb5Up8v” or “track:2NtFcdHF5rtDkWyNb5Up8v” but I still get errors like this:
Expected a list of Track, not u’uri’
HALP. Thanks.
The behaviour of this depends on what callingConvention
you specified when creating your mopidy instance. You almost certainly want to using “by-position-or-by-name”.
if set to “by-position-or-by-name”, methods expect to be called either with an array of position arguments, like mopidy.foo.bar([null, true, 2]), or with an object of named arguments, like mopidy.foo.bar({id: 2}). The advantage of the “by-position-or-by-name” calling convention is that arguments with default values can be left out of the named argument object. Using named arguments also makes the code more readable, and more resistent to future API changes.
Assuming you are using “by-position-or-by-name”, it’s easy to then pass an object containing just the argument(s) you want to the function i.e.
var track = 'spotify:track:2NtFcdHF5rtDkWyNb5Up8v'
mopidy.tracklist.add({"uri":track}).then(function(data){
// more fun here such as playing the added the TlTrack
});
If you have not already seen it, you might find the API-Explorer useful.
Next time, please provide the actual error message, and maybe your code too. Thanks.
Ahh… yep. that was it. I was unclear on what callingConvention exactly did. Thanks!