Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

214
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda