I'm putting together a nodejs application to retrieve my awards stuff from a website I frequent and having an issue with getting it to work. I'm trying to find out how I can pass the topic variable as an argument to my profile.get function.
Attempted the following:
users.forEach(profile.get(topic));
Result:
users.forEach(profile.get(topic));
^
TypeError: undefined is not a function
at Array.forEach (native)
app.js
var profile = require("./profile.js");
var topic = process.argv.slice(2,3);
var users = process.argv.slice(3);
users.forEach(profile.get);
profile.js
function get(username, topic) {
//Connect to API URL (.json)
var request = https.get("/" + username + ".json", function(response) {
var body = "";
//Read the data
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (response.statusCode == 200) {
try {
//Parse the data
var profile = JSON.parse(body);
//Print the data
printer.printMessage(username, profile.badges.length, profile.points.topic, topic);
} catch(error) {
//Parse Error
printer.printError(error);
}
} else {
//Status Code Error
printer.printError({message: "There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"});
}
});
});
//Connection Error
request.on('error', printer.printError);
}
Update:
console.log(users);
returns [ 'myuserrname', 'secondusernamehere' ]
I'm putting together a nodejs application to retrieve my awards stuff from a website I frequent and having an issue with getting it to work. I'm trying to find out how I can pass the topic variable as an argument to my profile.get function.
Attempted the following:
users.forEach(profile.get(topic));
Result:
users.forEach(profile.get(topic));
^
TypeError: undefined is not a function
at Array.forEach (native)
app.js
var profile = require("./profile.js");
var topic = process.argv.slice(2,3);
var users = process.argv.slice(3);
users.forEach(profile.get);
profile.js
function get(username, topic) {
//Connect to API URL (http://url./username.json)
var request = https.get("https://url./" + username + ".json", function(response) {
var body = "";
//Read the data
response.on('data', function(chunk) {
body += chunk;
});
response.on('end', function() {
if (response.statusCode == 200) {
try {
//Parse the data
var profile = JSON.parse(body);
//Print the data
printer.printMessage(username, profile.badges.length, profile.points.topic, topic);
} catch(error) {
//Parse Error
printer.printError(error);
}
} else {
//Status Code Error
printer.printError({message: "There was an error getting the profile for " + username + ". (" + http.STATUS_CODES[response.statusCode] + ")"});
}
});
});
//Connection Error
request.on('error', printer.printError);
}
Update:
console.log(users);
returns [ 'myuserrname', 'secondusernamehere' ]
Share Improve this question edited Feb 29, 2016 at 14:43 user3732216 asked Feb 29, 2016 at 14:37 user3732216user3732216 1,5899 gold badges31 silver badges56 bronze badges 1-
3
You have to wrap that call in a function, because you need to pass a function to
.forEach()
. It's unclear exactly what the code should look like because it's unclear what's in theusers
array and how its contents relate to the calls to thatprofile.get()
function. – Pointy Commented Feb 29, 2016 at 14:39
1 Answer
Reset to default 7If users
contains the user names to be passed to that .get()
function, then your loop would look like this:
users.forEach(function(username) {
profile.get(username, topic);
});
The .forEach()
method invokes your callback function, passing in succession each value in the array. If the values are the usernames, then each invocation of your callback will give you one username. Assuming that the topic
value is something defined outside the code you posted, it will be visible inside the callback too.
In your attempt, you were directly calling profile.get()
and passing its return value to .forEach()
. The function has no return value, so that's why .forEach()
threw that exception — the callback value you passed was undefined
.
In your previous question about this code, you were working with a version of that .get()
function that only took one parameter. Because of that, using
users.forEach(profile.get);
worked fine, because you passed a reference to the .get()
function to .forEach()
, and so it worked. However in this code:
users.forEach(profile.get(topic));
that profile.get(topic)
is a call to the function. That's what caused the problem. In JavaScript, the way to get around an issue like that (at least, the simplest immediate way) is to introduce that wrapper function at the top of this answer. Now, .forEach()
is happy, because you're passing it a function to call, and profile.get()
is happy because you're passing both the parameters it expects.