Convert Array to Query string in Meteor

白昼怎懂夜的黑 提交于 2019-12-11 08:36:12

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!