简体   繁体   English

类型错误:回调不是 function - 节点 JS

[英]TypeError: callback is not a function - Node JS

I am learning Node js and I get following error when run the code.我正在学习 Node js,运行代码时出现以下错误。

TypeError: callback is not a function类型错误:回调不是 function

Any help will be appreciated.任何帮助将不胜感激。

Following is my code以下是我的代码

console.log('before');
getUser(1, getRepositories);
console.log('After');


function getRepositories(user) {
    getRepositories(user.gitHubUsername, getCommits);
}

function getCommits(repos) {
    getCommits(repos, displayCommits);
}

function displayCommits(commits) {
    console.log(commits);
}



function getUser(id, callback) {
    setTimeout(() => {
        console.log('Reading a user data from database...');
        callback({ id: id, gitHubUsername: 'Gary' });
    }, 2000);
}

function getRepositories(username, callback) {
    setTimeout(() => {
        console.log('Calling Github API....');
        callback(['repo1', 'repo2', 'repo3']);
    }, 2000);
}

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.这是因为当您从getUser调用回调时,您期望调用getRepositories(user)但实际上getRepositories(username, callback) print username on the console so you'll know.在控制台上打印用户名,这样你就知道了。

THERE'S NO OVERLOADING IN JAVASCRIPT. 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.另一方面,您的getCommits(repos)在没有任何基本条件的情况下连续调用自己。 you're likely to receive RangeError: Maximum call stack size exceeded .您可能会收到RangeError: Maximum call stack size exceeded

You should have different functions name.您应该有不同的函数名称。

If you are new to nodeJs or JavaScript, Now a days you dont need to learn callback, specially you dont need to write your code in such a way where you provide function as a parameter(which you will call at the end of your function).如果您是 nodeJs 或 JavaScript 的新手,现在您不需要学习回调,特别是您不需要以提供 function 作为参数的方式编写代码(您将在函数末尾调用) .

If you are pretty much familiar with the execution flow, You can write the functions sequentially instead of writing callbacks.如果您非常熟悉执行流程,您可以按顺序编写函数而不是编写回调。

Even if any function is having asynchronous code then you can use Promises and make use of async/await functionality.即使任何 function 具有异步代码,您也可以使用 Promises 并使用 async/await 功能。

callback will make your code little complex and hard to debug.回调将使您的代码变得不那么复杂且难以调试。

This is my opinion.这是我的意见。 The javascript used to work perfectly over the time with callbacks, But with recent additions of Promise. javascript 过去可以在回调中完美运行,但最近添加了 Promise。 async/await life becomes little easy. async/await 生活变得不那么容易了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM