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