Javascript callback function throws error "Callback is not a function" in firefox
It is because you are not always passing the callback into that method.
success: function (data) {
var items = "";
$.each(data, function (i, item) {
items += "<option value=\"" + item.Value + "\">" + item.Text + "</option>";
});
$(childClass).html(items);
$(childClass)[0].selectedIndex = 0;
if(callback) callback(); //check before calling it.
}
Javascript: TypeError: callback is not a function
In your version, you're calling func2()
without specifying the callback
function, which is a required argument. In your second example (with init
commented out), you're correctly specifying the callback
function in func2(function (res) { ... });
.
Is the below snippet something you're looking for?
const Parent = (function () {
const func1 = function () {
func2(function (res) {
console.log(res);
});
}
const func2 = function (callback) {
callback('abc'); // this is passing 'abc' to line 3 of this snippet
}
return {
init: function () {
func1();
// func2(); // you don't want to call "func2" here, as "func1" calls it
// Or you could run:
func2(function (res) {
console.log(res);
});
// But this makes "func1" redundant
}
};
});
Parent().init();
// Output
abc
TypeError: callback is not a function - Node JS
that's because when you're calling callback from getUser
you're expecting that getRepositories(user)
is called but actually getRepositories(username, callback)
is called. print username on the console so you'll know.
THERE'S NO OVERLOADING IN JAVASCRIPT.
So what you need to do is either change the function's name, or do something like
function getUser(id, callback) {
setTimeout(() => {
console.log('Reading a user data from database...');
// callback({ id: id, gitHubUsername: 'Gary' });
callback("Gary", getCommits);
}, 2000);
}
On another note, your getCommits(repos)
continuously calls itself without any base condition. you're likely to receive RangeError: Maximum call stack size exceeded
.
callback is not a function (but it is)
You forget to call and handle the callback.
user.checkIfUserExists(newUser, function(arg1, isUserExist) {
if (isUserExist) {
//user exist
}
});
I am getting this error: 'TypeError: callback is not a function', but the function is still executing
module.exports = getData();
You are accidentally calling your function over here. Since no arguments are passed to it at this point, callback
is undefined, and so is not a function.
getting error callback is not a function in one condition
In your function, you first need to check if callback
is actually a function:
function EditorDuplicate(TransactionId, callback) {
// your code
if (typeof callback === 'function') {
callback();
}
}
This is because callback
becomes undefined
when it is not passed in this call:
EditorDuplicate(TransactionId);
And trying to call undefined
will throw an error.
Error callback is not a function in node.js
I'm unable to reproduce what you're seeing. Running your code under a couple of different conditions, I got the following results:
> var lobbies = [{ lobbyName: 'foo' }];
> doesLobbyExist('foo', console.log)
foo exists!
true
> doesLobbyExist('bar', console.log)
...
> var lobbies = [{ lobbyName: 'foo' }, { lobbyName: 'bar' }, { lobbyName: 'foo' }];
> doesLobbyExist('bar', console.log)
bar exists!
true
> doesLobbyExist('foo', console.log)
foo exists!
true
foo exists!
true
...
> var lobbies = [];
> doesLobbyExist('foo', console.log)
false
but there a couple of problems in your code:
lobbyExists
only gives afalse
response if there are no lobbies to check, it's possible for it to call the callback multiple times if there are multiple lobbies with the same name, and it doesn't return anything if a lobby isn't found.- there's no easy way to break out of
forEach
. since you only want to call your callback function once, you'll want to eitherreturn
orbreak
once a lobby has been found. switching over to afor i
loop allows us to do either one. - comparison using == is what's known as loose or abstract equality, which may cause errors down the road - you can read up on the difference between loose (==) and strict (===) equality here.
- if the
lobbyExists
function is only iterating through an array, then there's no need to treat it as asynchronous and use a callback. synchronous callbacks are a thing, and are perfectly fine to use, but it's something to be aware of as you continue to develop.
with all that being said, these are the changes that I would suggest:
function doesLobbyExist(a, callback) {
lobbyExists(a, function(random_data) {
//possibly do other things to random_data...
callback(random_data);
});
}
function lobbyExists(a, callback) {
for(var i = 0; i < lobbies.length; i++) {
var l = lobbies[i];
if(l.lobbyName === a) {
console.log(a + " exists!");
callback(true);
return;
}
}
callback(false);
}
Related Topics
How to Validate a Credit Card Number
Slick Slider - How to Keep the Active Pagination (Dot) Always Centered
Is There a Way That I Can Check If a Data Attribute Exists
Cannot Set Property ... Which Only Has Getter (Javascript Es6)
Why Am I Getting an Expected an Assignment or Function Call and Instead Saw an Expression
How to Remove Duplicates Elements Between 2 Arrays in Angular
How to Display Base64 Encoded Pdf
How to Sort Json Array Elements in Descending Order
How to Check If Prevprops and Nextprops Are the Same in React
How to Load Script in React Component
Ajax - How to Use a Returned Array in a Success Function
"Cannot Read Property of Undefined" When Using Chrome.Tabs or Other Chrome API in Content Script
How to Get Dynamic Id Attribute for a Button Which Is Inside a 'While' Loop
How to Import a Script into .Vue File
New Line in Paragraph When Enter Is Pressed in Textarea
Enabling/Disabling Inputs Based on Checkbox Activity
How to Make Mongoose Not Insert Empty Array or Object Fields into a Document