Javascript Undefined forEach TypeError:Undefined不是函数
我正在组装一个nodejs应用程序,从一个我经常访问的网站上检索我的获奖资料,但在让它正常工作时遇到了问题。我试图找出如何将topic变量作为参数传递给profile.get函数 尝试了以下操作:Javascript Undefined forEach TypeError:Undefined不是函数,javascript,node.js,Javascript,Node.js,我正在组装一个nodejs应用程序,从一个我经常访问的网站上检索我的获奖资料,但在让它正常工作时遇到了问题。我试图找出如何将topic变量作为参数传递给profile.get函数 尝试了以下操作: users.forEach(profile.get(topic)); 结果: users.forEach(profile.get(topic)); ^ TypeError: undefined is not a function at Array.forEach (native)
users.forEach(profile.get(topic));
结果:
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);
function get(username, topic) {
//Connect to API URL (http://url.com/username.json)
var request = https.get("https://url.com/" + 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);
}
profile.js
var profile = require("./profile.js");
var topic = process.argv.slice(2,3);
var users = process.argv.slice(3);
users.forEach(profile.get);
function get(username, topic) {
//Connect to API URL (http://url.com/username.json)
var request = https.get("https://url.com/" + 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);
}
更新:
console.log(用户)
返回['MyUserName','SecondUserName here']如果
用户
包含要传递给该.get()
函数的用户名,则循环如下所示:
users.forEach(function(username) {
profile.get(username, topic);
});
.forEach()
方法调用回调函数,连续传递数组中的每个值。如果这些值是用户名,那么每次回调调用都会给您一个用户名。假设topic
值是在您发布的代码之外定义的,那么它也将在回调中可见
在您的尝试中,您直接调用了profile.get()
,并将其返回值传递给.forEach()
。该函数没有返回值,因此.forEach()
引发了该异常-您传递的回调值是未定义的
在中,您使用的是只接受一个参数的.get()
函数的版本。因此,使用
users.forEach(profile.get);
工作正常,因为您将对.get()
函数的引用传递给了.forEach()
,因此它工作正常。但是,在本规范中:
users.forEach(profile.get(topic));
profile.get(topic)是对函数的调用。这就是问题的原因。在JavaScript中,解决这样一个问题的方法(至少是最简单的直接方法)是在这个答案的顶部引入包装器函数。现在,
.forEach()
很高兴,因为您传递了一个要调用的函数;profile.get()
很高兴,因为您传递了它期望的两个参数。您必须将该调用包装在一个函数中,因为您需要将函数传递给.forEach()
。不清楚代码到底应该是什么样子,因为不清楚用户
数组中有什么,以及它的内容与调用该配置文件.get()
函数的关系。是的,也许解释一下它为什么对他不起作用也会有帮助<例如,code>profile.get需要返回一个函数。它的工作方式与我决定添加主题之前的工作方式相同,这样我就不必将用户名传入forEach,我只需调用它,它就会知道了。@user3732216您不会将用户名传入.forEach()
调用.forEach()
将用户名传递到函数中。@Pointy检查一下我昨天做的这个堆栈问题,我不必传递用户名,它就可以工作了。那么,我如何才能通过该主题。@user3732216,因为您在该版本中只有一个参数,所以它可以工作。既然您必须传入主题
参数,那么您需要将.forEach()
所期望的API与实际获得的API相适应。