I've been going at a small snippet of code for like an hour or two now, but can't seem to figure out why my Javascript breaks down at the end of this code.
我现在已经花了一小时或两小时的代码,但似乎无法弄清楚为什么我的Javascript在这段代码的末尾出现故障。
// Getting username from address bar and user ID from username
var siteHref = window.location.href;
var specifiedUser = siteHref.split('#');
var userName = specifiedUser[1];
var userURL = 'http://soundcloud.com/' + userName;
var idGetter = SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
});
});
I've added the variables at the top for a bit of context. To explain this function passes the two parts of the array that I've specified into the console, which is perfect. Only when I call the variable is shows up as undefined?
我已经在顶部添加了变量以获得一些上下文。为了解释这个函数,我将指定的数组的两个部分传递给控制台,这是完美的。只有当我调用变量时才显示为未定义?
I also tried wrapping this in an additional function and has no luck calling it.
我也尝试将其包装在一个额外的功能中,并且没有运气称它。
Anyone have idea where I've gone wrong?
任何人都知道我哪里出错了?
4 个解决方案
#1
idGetter
isn't a function, you can't call it. If you want it to be a function, write:
idGetter不是一个函数,你不能称之为。如果你想要它是一个函数,写:
var idGetter = function () {
SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
});
});
};
Then you can do idGetter()
to run the function.
然后你可以用idGetter()来运行这个函数。
#2
You are calling two asynchronous functions. The results you get in the second one are ONLY valid inside that callback function or in a function you call from there. You can't use them globally afterwards because the async functions have not yet finished yet and thus they are still undefined
.
您正在调用两个异步函数。您在第二个结果中获得的结果仅在该回调函数内或您从那里调用的函数中有效。之后您无法全局使用它们,因为异步函数尚未完成,因此它们仍未定义。
Plus, if you're asking about the userInfo
variable, that goes out of scope as soon as the callback returns so it is not available anywhere else.
另外,如果您询问userInfo变量,一旦回调返回,该变量就会超出范围,因此在其他任何地方都无法使用。
See the comments I added to your code:
查看我添加到您的代码中的评论:
// Getting username from address bar and user ID from username
var siteHref = window.location.href;
var specifiedUser = siteHref.split('#');
var userName = specifiedUser[1];
var userURL = 'http://soundcloud.com/' + userName;
var idGetter = SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
// ** you must use userInfo here or call some other function
// ** and pass userInfo to it
});
});
// ** you cannot use userInfo here as it is both out of scope and
// ** has not yet been set
The idGetter
value will contain whatever the first call to SC.get()
returns which will NOT be the eventual asynchronous result. If you tell us what SC.get()
is, then we might be able to help you understand what it returns.
idGetter值将包含对SC.get()的第一次调用返回的内容,这将不是最终的异步结果。如果你告诉我们SC.get()是什么,那么我们可以帮助你理解它返回的内容。
If SC.get()
is the SoundCloud function, then it appears that it returns nothing and thus that is why idGetter
is undefined
.
如果SC.get()是SoundCloud函数,那么它似乎没有返回任何内容,因此这就是idGetter未定义的原因。
FYI, I confirmed here in the SoundCloud source that SC.get()
does not return anything.
仅供参考,我在SoundCloud源中确认SC.get()不会返回任何内容。
If all you're trying to do is to create a new function that contains all this code and will call a callback when the results are available, you can just define that function and then call it:
如果你要做的就是创建一个包含所有这些代码的新函数,并在结果可用时调用回调,你可以定义该函数然后调用它:
function getUserInfo(callback) {
// Getting username from address bar and user ID from username
var siteHref = window.location.href;
var specifiedUser = siteHref.split('#');
var userName = specifiedUser[1];
var userURL = 'http://soundcloud.com/' + userName;
SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
// ** you must use userInfo here or call some other function
// ** and pass userInfo to it
callback(userInfo);
});
});
// ** you cannot use userInfo here as it is both out of scope and
// ** has not yet been set
}
// then, you can call it like this:
getUserInfo(function(userData) {
// you can use userData here
});
#3
I can only assume that userInfo[2]
is showing as undefined since it would not make since for userInfo
to be undefined. If that is the case, then it is probable that user.public_favorites_count
is undefined. Trying printing that out to the console, and when it shows up as undefined, you'll need to determine why that is happening. Without more information about what that object is, or the code returning that object, there's not much more help the community will be able to provide.
我只能假设userInfo [2]显示为未定义,因为它不会生成,因为userInfo未定义。如果是这种情况,则很可能未定义user.public_favorites_count。尝试将其打印到控制台,当它显示为未定义时,您需要确定为什么会发生这种情况。如果没有关于该对象是什么的更多信息,或者返回该对象的代码,社区将无法提供更多帮助。
#4
This seems to either be working, or is a step forward.
这似乎要么有效,要么向前迈出一步。
var idGetter = function () { SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
});
});
};
var idGetter = idGetter();
Setting the variable before the function, and re-setting it after. Not sure if this is standard practise, but seems to work to some extent.
在函数之前设置变量,然后重新设置它。不确定这是否是标准做法,但似乎在某种程度上有效。
#1
idGetter
isn't a function, you can't call it. If you want it to be a function, write:
idGetter不是一个函数,你不能称之为。如果你想要它是一个函数,写:
var idGetter = function () {
SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
});
});
};
Then you can do idGetter()
to run the function.
然后你可以用idGetter()来运行这个函数。
#2
You are calling two asynchronous functions. The results you get in the second one are ONLY valid inside that callback function or in a function you call from there. You can't use them globally afterwards because the async functions have not yet finished yet and thus they are still undefined
.
您正在调用两个异步函数。您在第二个结果中获得的结果仅在该回调函数内或您从那里调用的函数中有效。之后您无法全局使用它们,因为异步函数尚未完成,因此它们仍未定义。
Plus, if you're asking about the userInfo
variable, that goes out of scope as soon as the callback returns so it is not available anywhere else.
另外,如果您询问userInfo变量,一旦回调返回,该变量就会超出范围,因此在其他任何地方都无法使用。
See the comments I added to your code:
查看我添加到您的代码中的评论:
// Getting username from address bar and user ID from username
var siteHref = window.location.href;
var specifiedUser = siteHref.split('#');
var userName = specifiedUser[1];
var userURL = 'http://soundcloud.com/' + userName;
var idGetter = SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
// ** you must use userInfo here or call some other function
// ** and pass userInfo to it
});
});
// ** you cannot use userInfo here as it is both out of scope and
// ** has not yet been set
The idGetter
value will contain whatever the first call to SC.get()
returns which will NOT be the eventual asynchronous result. If you tell us what SC.get()
is, then we might be able to help you understand what it returns.
idGetter值将包含对SC.get()的第一次调用返回的内容,这将不是最终的异步结果。如果你告诉我们SC.get()是什么,那么我们可以帮助你理解它返回的内容。
If SC.get()
is the SoundCloud function, then it appears that it returns nothing and thus that is why idGetter
is undefined
.
如果SC.get()是SoundCloud函数,那么它似乎没有返回任何内容,因此这就是idGetter未定义的原因。
FYI, I confirmed here in the SoundCloud source that SC.get()
does not return anything.
仅供参考,我在SoundCloud源中确认SC.get()不会返回任何内容。
If all you're trying to do is to create a new function that contains all this code and will call a callback when the results are available, you can just define that function and then call it:
如果你要做的就是创建一个包含所有这些代码的新函数,并在结果可用时调用回调,你可以定义该函数然后调用它:
function getUserInfo(callback) {
// Getting username from address bar and user ID from username
var siteHref = window.location.href;
var specifiedUser = siteHref.split('#');
var userName = specifiedUser[1];
var userURL = 'http://soundcloud.com/' + userName;
SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
// ** you must use userInfo here or call some other function
// ** and pass userInfo to it
callback(userInfo);
});
});
// ** you cannot use userInfo here as it is both out of scope and
// ** has not yet been set
}
// then, you can call it like this:
getUserInfo(function(userData) {
// you can use userData here
});
#3
I can only assume that userInfo[2]
is showing as undefined since it would not make since for userInfo
to be undefined. If that is the case, then it is probable that user.public_favorites_count
is undefined. Trying printing that out to the console, and when it shows up as undefined, you'll need to determine why that is happening. Without more information about what that object is, or the code returning that object, there's not much more help the community will be able to provide.
我只能假设userInfo [2]显示为未定义,因为它不会生成,因为userInfo未定义。如果是这种情况,则很可能未定义user.public_favorites_count。尝试将其打印到控制台,当它显示为未定义时,您需要确定为什么会发生这种情况。如果没有关于该对象是什么的更多信息,或者返回该对象的代码,社区将无法提供更多帮助。
#4
This seems to either be working, or is a step forward.
这似乎要么有效,要么向前迈出一步。
var idGetter = function () { SC.get('/resolve', { url: userURL }, function(user) {
SC.get('/users/', function() {
var userInfo = new Array(3);
userInfo[0] = user.username;
userInfo[1] = user.id;
userInfo[2] = user.public_favorites_count;
console.log(userInfo);
console.log(userInfo[2]);
});
});
};
var idGetter = idGetter();
Setting the variable before the function, and re-setting it after. Not sure if this is standard practise, but seems to work to some extent.
在函数之前设置变量,然后重新设置它。不确定这是否是标准做法,但似乎在某种程度上有效。