Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

215
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda