问题
I am trying to get a artist profile from Echonest. I need to have a parameter named 'bucket' multiple times in the query string. I am trying to set it with an array with the object that I am passing. Is this possible to pass this in an array?
I have this:
bucket:['biographies', 'images', 'artist_location', 'urls'];
I want this:
bucket=biographies&bucket=images&bucket=artist_location&bucket=urls
Client:
getArtistProfile = function(artistName){
var params = {
format:'json',
bucket:['biographies', 'images', 'artist_location', 'urls'],
name:artistName
};
Meteor.call('getEchoNestData', params, function(error, result) {
if (error)
console.warn(error);
else
console.log(result);
});
};
Server Method:
getEchoNestData:function(type, params){
check(type, String);
params.api_key = Meteor.settings.echonest.apiKey;
var result = HTTP.get('http://developer.echonest.com/api/v4/artist/profile' + type, {timeout:5000, params:params});
return result;
}
回答1:
I could be misinterpreting your question, but it seems like you are asking how to dynamically build a set of query string parameters. You could have a simple helper like this:
function getParams( arr ) {
var params = [];
for ( i = 0; i < arr.length; ++i ) {
params.push( 'bucket=' + arr[ i ] );
}
return params.join( '&' );
}
and pass in your array of param values like this:
var params = getParams( bucket );
http://jsfiddle.net/7vaLcjxs/
来源:https://stackoverflow.com/questions/25736718/convert-array-to-query-string-in-meteor