1

I simplified my code so you can help me get this right. I'm trying to call a function inside a function and the console says getId it's not a function. I really hope you can help!

/* Get username */
let getUsername = function() {
  return 'johndoe';
};

/* Get ID */
let getId = function() {
  console.log(getUsername());
  if (getUsername() == 'johndoe') {
    return 1;
  }
};

function getFollowerslist() {
  console.log(getId());
  return getId(getUsername);
};

console.log(getFollowerslist()); /* Error: getId is not a function */

EDIT :

I made a mistake when I simplified my code. The real issue is actually not the one found in my initial statement.

The issue is that my function getId() fetches the ID from an Instagram URL and returns it too late.

When I call getId() from inside getFollowerslist(), it returns undefined because getId() doesn't have a result yet.

/* Works every time */
let getId = function() {
    fetch("https://www.instagram.com/johndoe/?__a=1", {properties})
  .then(response => response.json())
  .then(data => {
      console.log('ID:' + data["graphql"]['user']['id']);
    return data["graphql"]['user']['id'];
  });
};

let getFollowers = function() {

    /* undefined */
    console.log('ID: ' + getId());

    /* R<est of the function doesn't work because getId returns undefined */
};

console.log(getId()); /* Works!!! */
console.log(getFollowers()); /* undefined */
Stefan Dacey
  • 165
  • 12
  • Does this answer your question? [What is the scope of variables in JavaScript?](https://stackoverflow.com/questions/500431/what-is-the-scope-of-variables-in-javascript) – Ivar Oct 21 '21 at 10:37
  • 3
    `getId` is the first parameter of `getFollowerslist(getId, getUsername) {}`. You're not passing any parameters to `getFollowerslist()` when you invoke it, so it doesn't get a value and is implicitly `undefined`. – Ivar Oct 21 '21 at 10:38
  • I tried to replace function getFollowerslist(getId, getusername) with let getFollowerslist = function(getId, getUsername) but getId returns undefined – Stefan Dacey Oct 21 '21 at 10:41
  • @StefanDacey Either remove the parameters from the method declaration, or pass them as parameters when you invoke the function. – Ivar Oct 21 '21 at 10:42
  • What do you return from `getId` when there's no match? – Andy Oct 21 '21 at 10:47
  • Hi guys, I just edited the original question with the original code because the issue is different than the one you found. getId() doesn't work when called from inside another function because it takes too much time fetching the result. Is that possible? – Stefan Dacey Oct 21 '21 at 11:37

2 Answers2

1

You just didn't fill in the arguments; do the following and it would run:

let getUsername = function() {
    return 'johndoe';
};

/* Get ID */
let getId = function(getUsername) {
    if (getUsername() == 'johndoe') {return 1;}
};

function getFollowerslist(getId, getUsername) {
    return getId(getUsername);
};

console.log(getFollowerslist(getId, getUsername));

You used getId from inside the function getFollowerslist, but didn't pass it when calling a function, and as a result, getId ended up being undefined. undefined, as you saw, isn't a function.
If you had just left the parameters part empty when declaring the getFollowerslist function, getId and getUsername wouldn't have been overridden and it would have worked. See below:

let getUsername = function() {
    return 'johndoe';
};

/* Get ID */
let getId = function(getUsername) {
    if (getUsername() == 'johndoe') {return 1;}
};

function getFollowerslist() {
    return getId(getUsername);
};

console.log(getFollowerslist());

EDIT:

Since you edited your question, here's my edited answer

let getId = async function() {
    let response = await fetch("https://www.instagram.com/johndoe/?__a=1",{ properties })
  response = response.json()
  return response;
};

let getFollowers = async function() {
    console.log('ID: ' + await getId());
};

console.log(getId()); /* Works!!! */
console.log(getFollowers()); /* undefined */

I've managed to get the response for you, but as I seem to be having some CORS issues, I don't know whether it is what you wanted. You need to tinker with the code (for example return response, it just returns the response, which for me seems to just be {}). For you, it just seems like you need to change it to return response["graphql"]['user']['id'].

Capt 171
  • 665
  • 5
  • 15
  • Hi there, thanks for your help! I just edited the original question with the original code because the issue is different than the one you found. getId() doesn't work when called from inside another function because it takes too much time fetching the result. Is that possible? – Stefan Dacey Oct 21 '21 at 11:37
  • I recommend using async/await instead of .then(). Also, try awaiting getId() – Capt 171 Oct 21 '21 at 11:47
  • I've never used await, how does it work? – Stefan Dacey Oct 21 '21 at 11:52
0

The problem is simple: you are using the same name for the function and the argument.

Inside this function scope:

function getFollowerslist(getId, getUsername) {
  console.log(typeof getId); //writes undefined
  return getId(getUsername);
};
console.log(typeof getId); //writes "function"

... the name "getId" refers to the argument (and not to the global function). Outside this function, "getId" is a function.

Just change the argument names, so they dont match the functions.

Wolfgang Amadeus
  • 398
  • 3
  • 12
  • Hi there, thanks for your help! I just edited the original question with the original code because the issue is different than the one you found. getId() doesn't work when called from inside another function because it takes too much time fetching the result. Is that possible? – Stefan Dacey Oct 21 '21 at 11:37
  • lol Now it is a totally different question. Previously you was using syncronous functions. Now you are using promises, and getId() is undefined because you are dealing with promises in a syncronous way. I humbly suggest you to write another question here (and mark me as Answer =D) – Wolfgang Amadeus Oct 21 '21 at 14:10