JSEvgeny
JSEvgeny

Reputation: 2750

How to pass variables to a method inside a fuction JavaScript

My code looks like this:

var getData = function(section, sort) {
    var url = 'https://api.imgur.com/3/gallery/+' section '+/+' sort '+/1'
    req.get({
        url: url,
        method: 'GET',
        headers: {
            'Authorization': 'Client-Id xxxxxxxxxxxxx'
        }
    }, function(e, r, body) {
        var metadata = JSON.parse(body);
        console.log(metadata.data.length);
    })
};
getData('hot', 'viral');

I'm sorry for the last part of the code, for some reason I couldn't make it better here.

My problem is that I wan't to firstly ask for search parameters(section, sort) and then use them in my url inside req.get method. Compiler gives me some:

 var url = 'https://api.imgur.com/3/gallery/+'section'+/+'sort'+/1';
SyntaxError: Unexpected identifier
at exports.runInThisContext (vm.js:53:16)
at Module._compile (module.js:373:25)
at Object.Module._extensions..js (module.js:416:10)
at Module.load (module.js:343:32)
at Function.Module._load (module.js:300:12)
at Function.Module.runMain (module.js:441:10)
at startup (node.js:139:18)
at node.js:968:3

I'm new to Node and JavaScript, please don't blame me. Thank You in advance!

Upvotes: 0

Views: 38

Answers (1)

adeneo
adeneo

Reputation: 318182

Replace

var url = 'https://api.imgur.com/3/gallery/+' section '+/+' sort '+/1'

with

var url = 'https://api.imgur.com/3/gallery/' + section + '/' + sort + '/1';

Upvotes: 3

Related Questions