Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

213
Views
Function in a function is not a function

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 */
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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'].

about 4 years ago · Juan Pablo Isaza Report

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.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!