我想使用回调函数的一个简单的例子,但它有一些问题。如何解决'回调不是函数错误'?
(function ddd() {
const categories = [];
const url2 = 'http://www.example.com';
const callback2 = function (res) {
console.log(res);
}
const callback = function (res,callback2) {
res.products.forEach((el) => {
categories.push({itemLabel: el.id, categoryLabel: 'gifts'});
});
callback2(categories);
};
getData(url2, callback);
})();
和控制台说callback2 is not a function
。
我试过console.log(typeof(callback2))
它说callback2
是一个函数。
那么,在这种情况下有什么问题?当我只输入console.log(categories)
时,效果很好。
哪里是你的'getData'功能? –
我没有写,因为它运作良好。任何问题? – ahnpersie