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

217
Views
Función en una función no es una función

Simplifiqué mi código para que puedas ayudarme a hacerlo bien. Estoy tratando de llamar a una función dentro de una función y la consola dice getId, no es una función. ¡Realmente espero que puedas ayudar!

 /* 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 */

EDITAR :

Cometí un error al simplificar mi código. El problema real en realidad no es el que se encuentra en mi declaración inicial.

El problema es que mi función getId() obtiene el ID de una URL de Instagram y lo devuelve demasiado tarde.

Cuando llamo a getId() desde dentro de getFollowerslist(), devuelve indefinido porque getId() aún no tiene un resultado.

 /* 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

Simplemente no completaste los argumentos; hacer lo siguiente y se ejecutaría:

 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));


getId desde dentro de la función getFollowerslist , pero no la pasaste al llamar a una función y, como resultado, getId terminó sin undefined . undefined , como viste, no es una función.
Si hubiera dejado la parte de parámetros vacía al declarar la función getFollowerslist , getId y getUsername no se habrían anulado y habría funcionado. Vea abajo:

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

EDITAR:

Ya que editaste tu pregunta, aquí está mi respuesta editada

 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 */

Logré obtener la respuesta para usted, pero como parece que tengo algunos problemas de CORS, no sé si es lo que quería. Debe jugar con el código (por ejemplo, return response , solo devuelve la respuesta, que para mí parece ser solo {} ). Para usted, parece que necesita cambiarlo para return response["graphql"]['user']['id'] .

about 4 years ago · Juan Pablo Isaza Report

0

El problema es simple: estás usando el mismo nombre para la función y el argumento.

Dentro del alcance de esta función:

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

... el nombre "getId" se refiere al argumento (y no a la función global). Fuera de esta función, "getId" es una función.

Simplemente cambie los nombres de los argumentos, para que no coincidan con las funciones.

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!